PIC2OLED Guest
|
PIC RS232 Transmission |
Posted: Wed Sep 09, 2009 10:08 am |
|
|
Hi I am using the pic16f818 and I am using the A/D converter to generate an 8bit number. I want to transmit that number via rs232. I am sure I connected everything fine, but somehow the PIC is sending 1s all the time.
And I have no clue at all what the problem might be, because before I send it I flash LEDs such that they represent the value and they flash properly i.e not all are lit up
Here is my code:
Code: |
#include <16F818.h>
#DEVICE ADC=8;
#use delay(clock=20000000)
#use rs232(BAUD=9600, XMIT=PIN_B2, RCV=PIN_B1, BITS=8, PARITY=N)
#fuses HS, NOWDT, NOBROWNOUT, NOPUT
unsigned int8 value; //declare variable value which is an 8 bit number
void main(void)
{
SET_TRIS_B(0b00001010); //0 means output, 1 means input; output pins B4 to B7 and B2
SET_TRIS_A(0b00100011); //0 means output, 1 means input;
output_bit(Pin_B4, 1); //just flashing LEDS at beginning to make sure it is all fine
output_bit(Pin_B5, 1);
output_bit(Pin_B6, 1);
output_bit(Pin_B7, 1);
output_bit(Pin_A2, 1);
output_bit(Pin_A3, 1);
output_bit(Pin_A4, 1);
output_bit(Pin_B0, 1);
delay_ms(1000);
output_bit(Pin_B4, 0);
output_bit(Pin_B5, 0);
output_bit(Pin_B6, 0);
output_bit(Pin_B7, 0);
output_bit(Pin_A2, 0);
output_bit(Pin_A3, 0);
output_bit(Pin_A4, 0);
output_bit(Pin_B0, 0);
delay_ms(1000);
output_bit(Pin_B4, 1);
output_bit(Pin_B5, 1);
output_bit(Pin_B6, 1);
output_bit(Pin_B7, 1);
output_bit(Pin_A2, 1);
output_bit(Pin_A3, 1);
output_bit(Pin_A4, 1);
output_bit(Pin_B0, 1);
printf("U"); //Send a U to the uOLED for baud detect
SETUP_ADC_PORTS(AN0); SETUP_ADC(ADC_CLOCK_INTERNAL);
delay_ms(10000);
do{
SET_ADC_CHANNEL(0);
delay_us(50);
value=READ_ADC(ADC_START_AND_READ); //start converting
delay_us(50);
putc(value); //send value
//flash LEDs such that they represent value
output_bit(Pin_B4, bit_test(value,0));
output_bit(Pin_B5, bit_test(value,1));
output_bit(Pin_B6, bit_test(value,2));
output_bit(Pin_B7, bit_test(value,3));
output_bit(Pin_A2, bit_test(value,3));
output_bit(Pin_A3, bit_test(value,5));
output_bit(Pin_A4, bit_test(value,6));
output_bit(Pin_B0, bit_test(value,7));
} while (TRUE);
} |
|
|