0
\$\begingroup\$

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
    }
} 
\$\endgroup\$
5
  • \$\begingroup\$ You assign TXREG the value 0x00. I believe that what you want to do is: TXREG = byte; \$\endgroup\$
    – m.Alin
    Commented Oct 4, 2011 at 17:28
  • \$\begingroup\$ Curiosity: What compiler are you using? It's not C18 or HI-TECH.. \$\endgroup\$
    – m.Alin
    Commented Oct 4, 2011 at 17:32
  • \$\begingroup\$ @Alin yeah it was a typo when I pasted the code, now TXREG=byte but it is again does not sending anything \$\endgroup\$
    – Dumbo
    Commented Oct 4, 2011 at 17:32
  • \$\begingroup\$ @Alin I am using CC5X \$\endgroup\$
    – Dumbo
    Commented Oct 4, 2011 at 17:33
  • 1
    \$\begingroup\$ try replacing TXREG = byte with TXREG = 0x55 and see if you see something useful on your scope... \$\endgroup\$
    – vicatcu
    Commented Oct 4, 2011 at 17:56

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.