Hello everyone,
I am trying to implement a half duplex rs232 communication link between my pic16f84a and my PC. It should be at 2400 baud, with 8N1 communication. The only pins on the rs232 connector that I have wired are the signal ground (which I have wired to ground), the data receive pin, and the data transmit pin which are both connected to the pic16f84a. I used the pins from this page http://www.arduino.cc/playground/uploads/Learning/rs232_pinout.gif, after verifying it from other pages, and assumed that the pin's parallel pin on the connector was it's corresponding pin. What I mean is that since the connector's pins are a mirror of the actual pins, I assumed each pin corresponded to it's mirrored pin. I am NOT using a UART so the signals are 0-5V. Pretty much nothing is happening on the computer end. I can get an LED to light up by connecting it to the data receive pin and sending data through /dev/ttyS0 but I could not read from it even after configuring the baud rate with stty speed 2400. Furthermore, whenever I would read from the PC with cat /dev/ttyS0 or while head -c 1 /dev/ttyS0; do : ;done the LED attached to the data receive pin on my breadboard would get much brighter. I tried inverting the bits and whatnot and that didn't work. So here is my code, compiled with SDCC in piklab. Also I'm using a 18.432 mHz oscillator. Any suggestions would be very helpful. Thanks!
I am trying to implement a half duplex rs232 communication link between my pic16f84a and my PC. It should be at 2400 baud, with 8N1 communication. The only pins on the rs232 connector that I have wired are the signal ground (which I have wired to ground), the data receive pin, and the data transmit pin which are both connected to the pic16f84a. I used the pins from this page http://www.arduino.cc/playground/uploads/Learning/rs232_pinout.gif, after verifying it from other pages, and assumed that the pin's parallel pin on the connector was it's corresponding pin. What I mean is that since the connector's pins are a mirror of the actual pins, I assumed each pin corresponded to it's mirrored pin. I am NOT using a UART so the signals are 0-5V. Pretty much nothing is happening on the computer end. I can get an LED to light up by connecting it to the data receive pin and sending data through /dev/ttyS0 but I could not read from it even after configuring the baud rate with stty speed 2400. Furthermore, whenever I would read from the PC with cat /dev/ttyS0 or while head -c 1 /dev/ttyS0; do : ;done the LED attached to the data receive pin on my breadboard would get much brighter. I tried inverting the bits and whatnot and that didn't work. So here is my code, compiled with SDCC in piklab. Also I'm using a 18.432 mHz oscillator. Any suggestions would be very helpful. Thanks!
Rich (BB code):
/* ----------------------------------------------------------------------- */
/* Template source file generated by piklab */
#include <pic16f877a.h>
#define VAR 0x2F
/* ----------------------------------------------------------------------- */
/* Configuration bits: adapt to your setup and needs */
typedef unsigned int word;
word at 0x2007 CONFIG = _HS_OSC & _WDT_OFF & _PWRTE_OFF & _BODEN_ON & _LVP_ON & _CPD_OFF & _WRT_OFF & _DEBUG_OFF & _CP_OFF;
unsigned volatile short time = 0;
unsigned volatile short time2 = 0;
unsigned char at VAR writeBuffer = 0;
unsigned char i;
void isr() interrupt 0 { /* interrupt service routine */
/* << insert interrupt code >> */
time++;
time2++;
if(time>20000)
{
_asm
bcf PORTA, 1
_endasm;
}else{
_asm
bsf PORTA, 1
_endasm;
}
if(time>40000)
time = 0;
T0IF = 0;
INTF = 0;
}
void setHigh()
{
_asm
bcf PORTB, 1
_endasm;
}
void setLow()
{
_asm
bsf PORTB, 1
_endasm;
}
unsigned char valueBit(unsigned char bit)
{
unsigned char t = 1;
t = t<<bit;
return (t & writeBuffer)==0;
}
void zerotime()
{
time2 = 0;
}
int getTime()
{
return time2*256+TMR0;
}
unsigned char inTime()
{
return time2*256+TMR0<1920;
}
void writeByte(unsigned char byte)
{
unsigned char tmp;
writeBuffer = byte;
tmp = valueBit(0);
zerotime();
setHigh();
while(inTime()){}
zerotime();
if(tmp)
setHigh();
else
setLow();
for(i = 0; i<7; i++)
{
tmp = valueBit(i+1);
while(inTime()){}
zerotime();
if(tmp)
setHigh();
else
setLow();
}
while(inTime()){}
zerotime();
setHigh();
while(inTime()){}
setLow();
}
void main() {
/* << insert code >> */
TRISA = 0x00;
TRISB = 0x01;
PORTA = 0x01;
writeBuffer = 0x00;
PORTB = 0x00;
T0CS = 0; // select internal clock
T0IE = 1; // enable timer interrupt
GIE = 1;
T0IF = 0;
INTF = 0;
while(1)
{
writeByte(0x5A);
zerotime();
setLow();
while(time2<10){}
}
}