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.

PIC18F some basic Begginers Coding questions in C

Status
Not open for further replies.

Dinnin

New Member
I am asking to start how i would assign the pin to read the voltage every second saved then read and saved again for as long as needed.

so here is what i got to start:

#define VSS PORTAbits.RA0 //this will be pin reading the voltage
#define ONN LATDbits.RD0 // this will be the pin to send the Hi
#define B1 PORTAbits.RA1
#define B2 PORTAbits.RA2 // these pins will monitor the buttons

so first i want to sent a hi from RD0 when button B1 is pressed and continue that Hi until the reading on RA0 is less than XXmvolts. So here is my very amateur shot in the dark i know very little and just trying to learn online please feel free to point out EVERYTHING that is stupid or wrong.

void main ( void )
{
for (B1=0) // I just need it to keep sending the HI after b1 is pressesd
{
While ( VSS < 23mV)
{
ONN = 1; // when i do this does it make the pin send a HI?
"what do i put here to take the reading from VSS?"
Delay10KTCYx ( 200 ) // Delay 1 second before next reading
}
}
}

So i know this must look very bad but i had to start somewhere, I realize i need somone other loop to monitor the buttons( no clue how to do that) i have no idea how to save the data on the pic so i am just starting small
I am using online tutorials if you have any awsome one please post, C only please.
 
Last edited:
I am asking to start how i would assign the pin to read the voltage every second saved then read and saved again for as long as needed.

so here is what i got to start:

#define VSS PORTAbits.RA0 //this will be pin reading the voltage
#define ONN LATDbits.RD0 // this will be the pin to send the Hi
#define B1 PORTAbits.RA1
#define B2 PORTAbits.RA2 // these pins will monitor the buttons

so first i want to sent a hi from RD0 when button B1 is pressed and continue that Hi until the reading on RA0 is less than XXmvolts. So here is my very amateur shot in the dark i know very little and just trying to learn online please feel free to point out EVERYTHING that is stupid or wrong.

void main ( void )
{
for (B1=0) // I just need it to keep sending the HI after b1 is pressesd
{
While ( VSS < 23mV)
{
ONN = 1; // when i do this does it make the pin send a HI?
"what do i put here to take the reading from VSS?"
Delay10KTCYx ( 200 ) // Delay 1 second before next reading
}
}
}

So i know this must look very bad but i had to start somewhere, I realize i need somone other loop to monitor the buttons( no clue how to do that) i have no idea how to save the data on the pic so i am just starting small
I am using online tutorials if you have any awsome one please post, C only please.

I know you want C but I think you really need to get the basics first.
https://www.electro-tech-online.com/custompdfs/2010/04/adc.pdf
https://www.electro-tech-online.com/custompdfs/2010/04/ADC20FUNCTIONS.pdf
 
This should get you started,
Code:
int ReadADC(unsigned char);    //prototype

void main (void){
    while(1){           //loop forever
        if(B1==0)       //if B1 pressed
        {
            While(ReadADC(0)>5){     //if A0 greater than 23mV
                ONN = 1;             //turn on LED
                //"what do i put here to take the reading from VSS?"
                Delay10KTCYx ( 200 ); // Delay 1 second before next reading 
             }
             ONN=0;	turn it off
        }
    }
}

int ReadADC(unsigned char Channel){
    ADCON0=(Channel<<2) | 1;
    ADCON2=0b10110101;          //Right justify - Fosc/16
    ADCON0bits.GO=1;            //start conversion
    while(ADCON0bits.GO);       //Wait for it to complete
    return ADRES;
}
Note you still need to set the LED pins to output (see triss register).

Edit, the value from the ADC is Voltage*1024/5 so in this case =0.023*1024/5 = 4.7

Mike.
 
Last edited:
ok so I did some looking and have some more questions on what you said.

Code:
#define VSS PORTAbits.RA0    // this pin will read the voltage 
#define ONN LATDbits.RD0    // This will send the HI to the FET to start Discharging 
#define B11 PORTAbits.RA1  // Buttons one
#define B22 PORTAbits.RA2 // Button 2

#define VSSTris TRISAbits.RA0 // ADDED this for Setting RA.0 at output



void main ( void ){

VSSTris = 0 ; //  IS THIS HOW I SET IT TO OUTPUT

  int ReadADC(unsigned char);    //prototype

	 while(1){           //loop forever
        if(B11==0)       //if B1 pressed
        {
                While(ReadADC(0)>5){     //if A0 greater than 23mV
                ONN = 1;                //turn on 
                Delay10KTCYx ( 200 );  // Delay 1 second before next reading 
             }
             ONN=0;	turn it off
        }
    }
}

int ReadADC(unsigned char Channel){
    ADCON0=(Channel<<2) | 1;
    ADCON2=0b10110101;          //Right justify - Fosc/16
    ADCON0bits.GO=1;            //start conversion
    while(ADCON0bits.GO);       //Wait for it to complete
    return ADRES;
}

So since the RA# pins are analog inputs what you did was set the ADC to use channel 0 and convert it and then return it to the ReadADC(unsigned char), more or less? if i may ask if the pin i was using was RA3 would that change the ADC code you wrote?

Also where in the data sheet would i find the voltage range the micro controller can read, for example 23-56mV i used 23 as an example because i am using a voltage divider to scale the voltage down but i am not sure the range I need to scale it to?
 
Last edited:
The function ReadADC will return a number between 0 and 1023. A value of 0 = 0V and 1023=5V and so a value of 1 is 5/1024V = 4.88mV. The value in the brackets is the channel number and so ReadADC(3) would read pin A3.

BTW, which pic chip are you using as the above code will not work with some of the newer ones.

Mike.
 
I am using the PIC18F452 as of right now or the PIC18F4520.

Thanks so much for your help with the voltage range it makes things so much easier. Its so simple, It was just dumb luck i chose the RA# pins after looking at the data sheet i noticed they where the Analog input pins.

Couple more questions, can i do math in the program for example if i read a value of 614 or 3V ( i think i did that right) if i wanted to display the actual 3V to my screen can i take the X= ReadADC(0)*5/1024 then i would use X to display that value to the screen?

Also not sure if you read the edit i made to set the RD0 as an OUTPUT here is the short version is this correct?

#define ONN LATDbits.RD0 // This will send the HI to the FET to start Discharging
#define ONNTris TRISDbits.RD0 // ADDED this for Setting RA.0 as output

ONNTris = 0;

is that the correct way aslong as the ONNTris = 0 is in my main
 
Last edited:
I am using the PIC18F452 as of right now or the PIC18F4520.

Thanks so much for your help with the voltage range it makes things so much easier. Its so simple, It was just dumb luck i chose the RA# pins after looking at the data sheet i noticed they where the Analog input pins.

Couple more questions, can i do math in the program for example if i read a value of 614 or 3V ( i think i did that right) if i wanted to display the actual 3V to my screen can i take the X= ReadADC(0)*5/1024 then i would use X to display that value to the screen?

Also not sure if you read the edit i made to set the RD0 as an OUTPUT here is the short version is this correct?

#define ONN LATDbits.RD0 // This will send the HI to the FET to start Discharging
#define ONNTris TRISDbits.RD0 // ADDED this for Setting RA.0 as output

ONNTris = 0;

is that the correct way aslong as the ONNTris = 0 is in my main

Yes and Yes. :D

Edit, it should be,
Code:
#define ONNTris TRISDbits.[COLOR="red"]TRIS[/COLOR]D0

Mike.
 
Last edited:
WOW AWSOMENESS!!! thanks so much, the project is really coming together anks so much for your help!! I got a few more question that i will have but i will post new topics i guess. I posted one about coding the buttons how i need them to be in the general forums not sure if you can answer than one if you want to give it a shot

https://www.electro-tech-online.com/threads/pic18f-using-buttons-c-codeing-help.106618/

Last question if i wanted to save each reading on the micro controller, yes i actually want it ON the micro controller do you know how i would save each reading of the voltage from the ADC?
 
Last edited:
In your other post you have while(b2=1) and it should be while(b2==1). You also have braces the wrong way round.

As for saving readings, you could save them in RAM, EEPROM or external EEPROM. How many readings need saving?

Mike.
 
Last edited:
I am not sure, what ever is easiest to access and port over to the computer when/if needed. I will be taking readings every 1 second for about an hour so that 3600 reading +/- 200. Whatever makes the code easiest is really all i care about, basically what ever you think is best is what i will do..
 
Last edited:
The only way to store that amount of data on the chip is to use the flash program memory and that gets rather complicated. See section 5 of the data sheet.

Mike.
 
OK i had thought so and people had suggested i just save the data to a thumb drive i will look into getting a USB port of some kind so i can plug a thumb drive in. Would it be possible to save the data on a thumb drive as it takes the readings?
 
Getting USB working on a pic is a nightmare and even more so as a host. You need to improve your programming skills before going anywhere near USB. I made one (clicky) a while ago and it's not trivial.

Mike.
 
Status
Not open for further replies.

Latest threads

Back
Top