Before connecting the PIC, check the supply voltages to ensure that the linear voltage regulator (or other power supply scheme) is supplying optimally 5V. Note (from datasheet), absolute maximum voltage on Vdd with respect to Vss is -0.3V to +7.5V.
In terms of the actual MAX232, ensure that the polarity of the electrolytic capacitors are correct, and that a 10uF (or similar value) by-pass capacitor is installed as close to the MAX232 as possible.
The TX pin of the MAX232 goes to the RX of the PIC and vice-versa.
Programming the PIC
The CCS compiler C compiler provides driver code for the PIC USART RS-232 which makes the firmware/software relatively straight forward. The following line of code is required:
#use rs232(baud=9600,parity=N,xmit=PIN_B6,rcv=PIN_B7,bits=8, ERRORS)
The #use rs232 directive provides the compiler the information about the RS-232 connection to be made, which includes (as is fairly obvious from the statement syntax) items such as baud rate, parity etc). The inclusion of the ERRORS directive is important. This instructs the compiler to "reset" the hardware USART due to buffer overrun conditions if they occur. This avoids "hanging" the USART.
The following code snippet demonstrates the code necessary to interface a PIC microcontroller with a PC to receive data (using a MAX232). The code includes an timer interrupt driven routine that flashes a LED at ~2 second intervals to provide feedback that the programme is running. When a character is received by the PIC micrcontroller (sent with a suitable terminal program on the PC), it is echoed back to the PC with a suitable status message.
Code Snippet 1:
#include <RS232.h>
#zero_ram //all variables automatically initialised to 0
int8 readCount = 0;
int8 toggleReadCount = 0;
#int_timer1
void timer1_interrupt() { /* Timer1 has wrapped around to 0. */
toggleReadCount=1;
set_timer1(3035); //prescaler=8, 10Mhz, preload=3036 -> Freq 5hz, period=0.2sec
}
void main() {
char recChar; //the received character from the serial port via keyboard
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); //prescaler 8
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
printf("\n\rstart\n\r");
while(TRUE){
if (toggleReadCount==1) {
toggleReadCount = 0;
readCount++; //10 counts of 0.2sec = read every 2 secs
}
if (readCount>=10) { //10 counts of 0.2sec = read every 2 secs
output_toggle(PIN_B0); //flash LED to show program active
readCount = 0;
toggleReadCount = 0;
set_timer1(3035); //prescaler=8, 10Mhz, preload=3036 -> Freq 5hz, period=0.2sec
}
if (kbhit()) { //if character received by RS232 then process data
recChar = getc();
printf("received=%c \n\r",recChar);
switch (recChar) {
case 'c':
case 'C': printf("cmd C\n\r");
break;
default: printf("Unknown cmd\n\r");
break;
}
}
}
}
Only Logged-In Members can add comments