I am trying to make my pic to send the value of the potentiometer (on PICKIT 1) through serial. At this step I am using an oscilloscope to see if it is doing well.
I wrote the code below, but nothing is changed on the scope screen when I move pot meter. Can you have a look please?
void delay_ms(uns16 millisec)
{
char next=0;
OPTION=2;
TMR0=1;
do {next+=125; while(TMR0!=next);}
while(--millisec!=0);
}
void main()
{
char byte;
int state = 1;
uns16 y=255;
TRISA=0b11000010; //RA1 is input
ANSEL=0b00000001; //analogue input ANO
CMCON0=7; //to switch off the comparator
TRISC.4=1; //USART will automatically configure input-output.
ADCON1=0b00010000; //Fosc/32
ADCON0=0b00000001; //reference voltage VDD
SPBRG=32; // the nearest int to achieve 9600 baud for 20MHZ
BRG16 =0; //8 bit generator.
BRGH=0; //recommended to be set to reduce baud rate error. clear is better.
SYNC=0;// select asychronous
SPEN=1;//serial port enable
ADCON0=0; //ANO selected
ADFM=0; //Left justified
ADCON0.0=1; //Enable conversion
TXEN=1; //Enable transmission
//delay_ms(y); //wait while he's converting
ADCON0.1=1; //GO but turned on.
while (1)
{
byte=ADRESH;
TXREG=byte;
delay_ms(100); //a short delay
}
}
Best Answer
You are enabling the "GO" bit, which performs one conversion of the ADC.
Once that conversion is complete the "GO" bit is turned off and the ADC does nothing else until the "GO" bit is turned on again.
You should have something more like this:
Or, a better format (compiler dependent):
And even better - check to see if it has changed and only send if it has:
And you should really check to see if the transmit buffer is ready to receive a new byte:
Of course, I have just knocked this code together in the browser here, so I haven't tested it, but it should give you the idea.