USART communication with PIC16F688

cpicpickitserialuart

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:

while (1)
{
    ADCON0.1=1;
    while(ADCON0.1==1);
    byte=ADRESH;
    TXREG=byte;
}

Or, a better format (compiler dependent):

while (1)
{
    ADCON0bits.GO=1;
    while(ADCON0bits.GO==1);
    byte=ADRESH;
    TXREG=byte;
}

And even better - check to see if it has changed and only send if it has:

unsigned char byte,old;

... setup code ...

while (1)
{
    ADCON0bits.GO=1;
    while(ADCON0bits.GO==1);
    byte=ADRESH;
    if(byte!=old)
    {
        old=byte;
        TXREG=byte;
    }
}

And you should really check to see if the transmit buffer is ready to receive a new byte:

unsigned char byte,old;

... setup code ...

while (1)
{
    ADCON0bits.GO=1;
    while(ADCON0bits.GO==1);
    byte=ADRESH;
    if(byte!=old)
    {
        old=byte;
        while(TCSTAbits.TRMT==0);
        TXREG=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.