Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Connecting pot to uc

Status
Not open for further replies.

Varunme

Member
is the below connection for pot with uC ok for real world , i connected in this way in real world but the analoge value changes at the same position of pot each time i switch on the board

View attachment 66831

Added a 4700uF/25V cap to powersupply, then also same
power supply is 9V/500mA DC
 
9V will destroy a PIC, I hope you meant you have 9V feeding a 5V regulator.

Failing that. as you've posted no code we don't have a clue what you're doing wrong?, but as you say it changes at one specific point, it may be that you're using the port as a digital input rather than an analogue one.

My tutorials show how to use the analogue inputs.
 
yes , its with VR

My code is as below

Code:
// LCD module connections
sbit LCD_RS at RD2_bit;  // for writing to output pin always use latch (PIC18 family)
sbit LCD_EN at RD3_bit;  // for writing to output pin always use latch (PIC18 family)
sbit LCD_D4 at RD4_bit;  // for writing to output pin always use latch (PIC18 family)
sbit LCD_D5 at RD5_bit;  // for writing to output pin always use latch (PIC18 family)
sbit LCD_D6 at RD6_bit;  // for writing to output pin always use latch (PIC18 family)
sbit LCD_D7 at RD7_bit;  // for writing to output pin always use latch (PIC18 family)

sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections

unsigned char ch;                    //
unsigned int adc_rd;                 // Declare variables
char *text;                          //
char junk[40];                       // bodge to get around error above
long tlong,ulong, tlong1;
char output[7];
int i,rot;
float ufloat,tfloat;
int uint;


void main() {
    CMCON |= 7;

    TRISA = 0x04;                    // RA2 is analogue input
    ADCON1 = 0x80;
    TRISB =0;                       //
    PORTB=0x00;

    Lcd_Init();                      // LCD display initialization

    Lcd_Cmd(_LCD_CLEAR);
    text = "Output Voltage";            // Define the second message
    Lcd_Out(1,1,text);               // Define the first message
    Delay_ms(1000);
    text = "voltage:";               // Define the third message

    while (1) {


        adc_rd = ADC_Read(2);        // A/D conversion. Pin RA2 is an input.
        Lcd_Out(2,1,text);           // Write result in the second line
        tlong = (long)adc_rd;  // Convert the result in millivolts

        ch = tlong / 1000;           // Extract volts (thousands of millivolts)
                                     // from result
        Lcd_Chr(2,9,48+ch);          // Write result in ASCII format
        Lcd_Chr_CP('.');
        ch = (tlong / 1000) % 10;     // Extract thousands of millivolts
        Lcd_Chr_CP(48+ch);
        ch = (tlong / 100) % 10;     // Extract hundreds of millivolts
        Lcd_Chr_CP(48+ch);           // Write result in ASCII format
        ch = (tlong / 10) % 10;      // Extract tens of millivolts
        Lcd_Chr_CP(48+ch);           // Write result in ASCII format
        ch = tlong % 10;             // Extract digits for millivolts
        Lcd_Chr_CP(48+ch);           // Write result in ASCII format
        Lcd_Chr_CP('V');
        Delay_ms(1);

        UART1_Init(9600);               // Initialize UART1 module at 9600 bps
        Delay_ms(100);                  // Wait for UART 1module to stabilize
        UART1_Write_Text("Start");
        UART1_Write(13);
        UART1_Write(10);



        while (!UART1_Data_Ready());        // wait for UART character in buffer

        // Initialize string with all '\0'
        for(i=0; i<7; i++){
            output[i] = '\0';
        }

        UART1_Read_Text(output, "m", 255);   // read input from uart
        ulong = atol(output);
        tlong1= tlong; ///1000;


        while(1){

           if ( ulong != tlong1 ){
                if ( ulong > tlong1 ){

                while(1)
                {
                // UART1_Write_Text("1a");
                Delay_ms(20);
                PORTB=0b00001100;
                Delay_ms(20);
                PORTB=0b00000110;
                Delay_ms(20);
                PORTB=0b00000011;
                Delay_ms(20);
                PORTB=0b00001001;
                adc_rd = ADC_Read(2);        // A/D conversion. Pin RA2 is an input.
                //Lcd_Out(2,1,text);           // Write result in the second line
                tlong = (long)adc_rd;  // Convert the result in millivolts
                tlong1= tlong;

                //UART1_Write_Text("1b");
                    if (tlong1 == ulong ){
                     UART1_Write_Text("Brk");
                                    break;
                }
                }
                }



                else{
                    while(1)
                {
                    //UART1_Write_Text("2a");
                    Delay_ms(20);
                    PORTB=0b00001001;
                    Delay_ms(20);
                    PORTB=0b00000011;
                    Delay_ms(20);
                    PORTB=0b00000110;
                    Delay_ms(20);
                    PORTB=0b00001100;
                     adc_rd = ADC_Read(2);        // A/D conversion. Pin RA2 is an input.
                    // Lcd_Out(2,1,text);           // Write result in the second line
                     tlong = (long)adc_rd;  // Convert the result in millivolts
                     tlong1= tlong;

                    //UART1_Write_Text("2b");
                       if ( tlong1 == ulong ){
                       UART1_Write_Text("Brk");
                       break;
                       }
                    }
                    }
                    }
                    else
                    { break;}
}
}
}
 
What chip you may be messing up pins you need for LCD . And what compiler
 
Last edited:
And what chip
This
ADCON1 = 0x80;
doesn't set all pins as you think the way you'ld need them could be reading to fast

But you not stating which chip your using
 
Last edited:
Let's put some light on this OK

First ADC_read dose this
Initializes PIC’s internal ADC module to work with RC clock. Clock determines the time period necessary for performing AD conversion (min 12TAD).

Now post what chip your using and I'll post you a code samlpe that works with your chip using a LCD
display the output.
 
Last edited:
This is with a 8 mhz crystal the configure is set in MiKroC IDE for OSC and Crystal type and configure word

Code:
 // LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections

unsigned int adc_rd;
char message1[] = "Voltage = ";
char *temp = "0000";

void main() {
 CMCON  |= 0x07;            // turn off comparators
  ADCON1 |= 0x0C;            // Set AN2 channel pin as analog
  TRISA2_bit = 1;            //   input
Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
 Delay_ms(1000);
   Lcd_Out(1,1,message1);           // Write message1 in 1st row


while (1) {


        adc_rd = ADC_Read(2);        // A/D conversion. Pin RA2 is an input.
        temp[0] = adc_rd/1000 + 48; // Add 48 to get the ASCII character value
        temp[1] = (adc_rd/100)%10 + 48;
        temp[2] = (adc_rd/10)%10 + 48;
        temp[3] = adc_rd%10 + 48;
         Lcd_Out(1,11,temp);

   Delay_ms(500);                     // lets you see it on the LCD

  }
}
 
Last edited:
It doesn't matter Just set your OSC speed in the IDE to your crystal

It's not set in the code just the port pins and registers for ADC and LCD
 
Last edited:
Well I'm sure the code should work if you have the wiper to the right pin
Now if you hooked volts higher then vdd you could short to vdd
Check the pot
 
Ok I see whats happening here change ADCON1 |= 0x0C; to ADCON1 |= 0x09;
Your reading VDD because AN2 is tied to VDD when set to 0x0C

It works fine pic.
View attachment 66968
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top