Problems with Microcontroller Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you the Codes for Registered Members of this site only, at free service...Follow Me.

User Inputs from Tact Switches and Software UART

In this experiment, we will demonstrate how to read inputs from tact switches, and then will also show how to send character data from PIC12F683 to a desktop PC Hyper-Terminal program through UART communication. Remember that PIC12F683 doesn't have hardware UART module, so we are going to use the Soft_UART library routines from mikroC for this purpose. Regarding the Hyper-Terminal setup on PC, read this. Besides, to read the user inputs from tact switches, Button Library routines in mikroC will be used.

Snapshots of the required Library Routines from mikroC User's Manual


Setup:
The two switch inputs in our board will be connected to GP0 and GP1 port (Pins 7 and 6 respectively). GP5 (Pin 2) will be used for Software UART Tx. So here is what you need to do:
  • Connect SW1 and SW2 (tact switches) to GP0 and GP1 using jumper wires,
  • Connect GP5 to input Tx of TTL to RS232 Level Shifter circuit,
  • Connect Rx(2) and Gnd(5) pins of a RS232 port to the board.



Software:
This code will detect which of the two tact switches is pressed and then send a message to the PC through UART, e.g. if SW1 is pressed, it will say "SW1 Pressed".

/*
  PIC12F683 Experiment Board
  Experimen No. 2 : Input from tact switches and send info to
  desktop PC using Software UART.
  Date: 04/03/2010
*/
char Message1[] = "SW1 Pressed";
char Message2[] = "SW2 Pressed";
char i, error;
void main() {
CMCON0 = 7;
TRISIO = 11;  // GPIO 0, 1, 3 Inputs; Rest are O/Ps
ANSEL = 0;
GPIO = 0;
// Define GPIO.3 as UART Rx, and 5 as Tx
error = Soft_UART_Init(&GPIO,3, 5, 9600, 0 );
Delay_ms(100);

do {
   if (Button(&GPIO, 0, 1, 0)) {  // Detect logical one to zero
      Delay_ms(300);
      for (i=0; i<= 10; i++) {
      Soft_UART_Write(Message1[i]);
      Delay_ms(50);
     }
     Soft_UART_Write(10); // Line Feed
     Soft_UART_Write(13); // Carriage Return
      }
    if (Button(&GPIO, 1, 1, 0)) {  // Detect logical one to zero
      Delay_ms(300) ;
      for (i=0; i<= 10; i++) {
      Soft_UART_Write(Message2[i]);
      Delay_ms(50);
     }
     Soft_UART_Write(10); // Line Feed
     Soft_UART_Write(13); // Carriage Return
    }

 } while(1);
}

Output: 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Recent Comments

Popular Projects

Give Support

Give Support
Encourage Me through Comments

Microcontroller Projects

Total Pageviews