how to programme using pickit 3

chrisw1990

Joined Oct 22, 2011
551
with 5V supply? thats not much current...
id expect say 370R for LEDs and as your usign common anode display, the same.. 370R for the 7-seg disp too.:)
 

Thread Starter

harami

Joined Jun 20, 2011
66
with 5V supply? thats not much current...
id expect say 370R for LEDs and as your usign common anode display, the same.. 370R for the 7-seg disp too.:)
i really dont know why this does not work.. its really challanging but i just got 1 more week to make it work and i dont seem to find the problem.. i got the code, i got the circuit and i have successfully programmed my pic still i get nothing when i test it.. if anyone could help me out, i would be reall thankful. all of youhave help me a lot but nothing seems to be working. so please can anyone help me out. :(
 

Thread Starter

harami

Joined Jun 20, 2011
66
have you changed the resistors? the resistance is too high =] replace them with 370R.
post the code too?
yes i changed the resistors to 390R because 370R was not available however it will arrive within 2 days. and i have also changed the MCLR as you showed. below is the code and i have configured all the bits in the setting which i have attached a picture:

Rich (BB code):
#include <pic18f4520.h>

//Defining constants for sensors
#define SensA0 RC0
#define SensB0 RC1
#define Sens1 RC2
#define Sens2 RC3
#define Sens3 RC4
#define Sens4 RC5
#define Sens5 RC6

//Number of timer overflows in 1 minute
#define Minute 120

//Variables, that stores value of Ports B and D
char PortB;
char PortD;

//Number of free bays
int FreeBay = 5;

//Which bays are occupied
char BayOc[5] = {0};

//Function to get value of sensors in each bay
char GetSens(char i);

//Function to activate LEDs in each bay
void SetLed(char n, char m);

void main(void)
{
    //Setting each port to be input or output
    TRISA = 0;
    TRISB = 0;
    TRISC = 0xFF;
    TRISD = 0;

    //Timer settings
    TMR0ON = 0; //Off timer
    TMR0IF = 0; //Reset it
    TMR0H = 0xB; //Set start value of high timer bit
    TMR0L = 0xDC; //Set start value of low timer bit
    T016BIT = 0; //16 - bit mode
    T0CS = 0; //Internal clock
    T0PS3 = 0; //Use prescaler
    //Set prescale 1:8
    T0PS2 = 0;
    T0PS1 = 1;
    T0PS0 = 0;

    TMR0ON = 1; //Timer On

    //Flags
    char flag1 = -1;
    char flag2 = 0;

    //What time the car stay in the bay
    signed char TimeIn[5] = {-1,-1,-1,-1,-1};

    char i;

    //Start value for LEDs (all is green)
    PORTB = PortB = 0b0010010;
    PORTD = PortD = 0111;

    //Output number of free bays
    PORTA = FreeBay;
    
    //Main loop
    while (1)
    {

        //If Sensor A is activates but Sensor B isn't
        if (SensA0 == 1 && SensB0 == 0 && flag1 != 2)
        {
            flag1 = 2;

            //If sensor B has already been passed
            if (flag2 == 2)
            {
                if (FreeBay<5)
                {
                    FreeBay++;
                    PORTA = FreeBay;
                }
                flag2 = 0;
            }
            else
                flag2 = 1;
        }

        //If Sensor B is activates but Sensor A isn't
        if (SensA0 == 0 && SensB0 == 1 && flag1 != 1)
        {
            //If sensor A has already been passed
            flag1 = 1;
            if (flag2 == 1)
            {
                if (FreeBay>0)
                {
                    FreeBay--;
                    PORTA = FreeBay;
                }
                flag2 = 0;
            }
            else
                flag2 = 2;
        }
        
        //If sensors A and B aren't activated
        if (SensA0 == 0 && SensB0 == 0)
        {
            flag1 = 0;
        }

        //If sensors A and B are activated
        if (SensA0 == 1 && SensB0 == 1)
        {
            flag1 = 3;
        }

        //If timer is overflowed
        if (TMR0IF == 1)
        {
            //Reset timer
            TMR0IF = 0;
            TMR0H = 0xB;
            TMR0L = 0xDC;
            
            //Check all sensors in bays
            for (i=0;i<5;i++)
            {

                //If it's in "yellow" state
                if (TimeIn>=0)
                {
                    TimeIn += 1;
                }

                //If sensor is active and bay wasn't occupied
                if (GetSens(i) == 1 && BayOc == 0)
                {
                    BayOc = 1;
                    TimeIn = 0;
                    SetLed(i,1);
                    continue;
                }

                //If sensor is not active and bay was occupied
                if (GetSens(i) == 0 && BayOc == 1)
                {

                    BayOc = 0;
                    if (TimeIn == -1)
                    {
                        SetLed(i,0);
                        continue;
                    }
                    TimeIn = -1;
                    SetLed(i,1);
                    continue;
                }

                //If 1 minute passed
                if (TimeIn>=Minute)
                {
                    SetLed(i,2);
                    TimeIn = -1;
                }
            }
        }
      
    }

 
}

//Function to get value of sensors in each bay
char GetSens(char i)
{
    switch (i)
    {
        case 0:
            return Sens1;
        case 1:
            return Sens2;
        case 2:
            return Sens3;
        case 3:
            return Sens4;
        case 4:
            return Sens5;
    }
}

//Function to activate LEDs in each bay
//n is number of bay, m is number of sensor (0-green, 1-yellow, 2-red)
void SetLed(char n, char m) 
{
    char value = 0;
    if (m == 0)
    {
        value = 0b101;
    }
    else
    {
        value = 0b11;
        value <<= m-1;
    }
    if (n==2 && m!=1)
    {
        PORTB = PortB ^= 1;
    }
    if (n<=2)
    {
        value <<= n*3;
        PORTD = PortD ^= value;
    }
    else
    {
        value <<= (n-3)*3+1;
        PORTB = PortB ^= value;
    }
}
 

Attachments

chrisw1990

Joined Oct 22, 2011
551
you still havent set the config bits.. see at the top "configuration bits set in code" uncheck it and set up the bits there.. makes things simple..
next problem... your trying to detect your ldrs as digital IO.. that wont work..well it will, but its wrong, and you shouldnt.
perform A/D conversions using the ADC. this will require you to swap to the ANx pins on PORT A (and therfore move your seven segment display to port C..
sorry but such is life.. the voltage required to trigger a "high"..
see this thread.. you will need to determine your desired trigger voltage, and then do your desired action when the AD result is above the threshold..:)
 

Thread Starter

harami

Joined Jun 20, 2011
66
you still havent set the config bits.. see at the top "configuration bits set in code" uncheck it and set up the bits there.. makes things simple..
next problem... your trying to detect your ldrs as digital IO.. that wont work..well it will, but its wrong, and you shouldnt.
perform A/D conversions using the ADC. this will require you to swap to the ANx pins on PORT A (and therfore move your seven segment display to port C..
sorry but such is life.. the voltage required to trigger a "high"..
see this thread.. you will need to determine your desired trigger voltage, and then do your desired action when the AD result is above the threshold..:)
do i have to uncheck the "configuration bits set in code" and set up the bits in my code or will it still work if i leave it as it is?? (just wanted to know. however i have configured the bits in my code).

about ADC, sorry but i didnt really get you there. are you saying that i need to modify my code and circuit diagram?? i know i have written and configured my programme in different (weird) way. as you have said
next problem... your trying to detect your ldrs as digital IO.. that wont work..well it will, but its wrong,
if it works, even its done in a wrong way thats fine for now. i will learn it and do it in a correct way. however because i dont have much time left, i want to find and correct the real problems which are the main reason for this project not to work. sorry for sounding this way but im worried about the amount of time i got left and amount of work i still got to do. :( this is my first project in c language, infact first project with microcontroller so due to lack in knowledge about programming language, im really struggling and sadly i dont have enough time to understand the c language aswell. so can you guys please help me out. :(
 

chrisw1990

Joined Oct 22, 2011
551
oh.. i didnt see any config bits in your code? so.. i assumed there werent any!
i think the problem is your config bits still. can you just copy the code where you have set them please?
 

Thread Starter

harami

Joined Jun 20, 2011
66
oh.. i didnt see any config bits in your code? so.. i assumed there werent any!
i think the problem is your config bits still. can you just copy the code where you have set them please?
i have done it this way. im not sure if i have done it correctly tho..
Rich (BB code):
#include <pic18f4520.h>

CONFIG WDT=OFF; 	//Disable watchdog timer 
CONFIG MCLRE = ON; 	//MCLEAR Pin on 
CONFIG DEBUG = ON;	//Enable Debug Mode 
CONFIG LVP = OFF; 	//Low-Voltage programming disabled (necessary for debugging) 
CONFIG OSC = LP 	//Low pass oscillator 
CONFIG FCMEN = OFF	//Fail-safe clock monitor disabled
CONFIG IESO = OFF	//Oscillator swtchover mode disabled
CONFIG PWRT = ON	//PWRT enabled
CONFIG BOREN = OFF	//Brown-out reset dissabled in hardware and software

//Defining constants for sensors
#define SensA0 RC0
#define SensB0 RC1
#define Sens1 RC2
#define Sens2 RC3
#define Sens3 RC4
#define Sens4 RC5
#define Sens5 RC6
 

Thread Starter

harami

Joined Jun 20, 2011
66
it compiles with that?!
do it like i told you!
Rich (BB code):
#pragma config WDT=OFF
is this ok??
Rich (BB code):
#include <pic18f4520.h>

#pragma CONFIG WDT=OFF; 	//Disable watchdog timer 
#pragma CONFIG MCLRE = ON; 	//MCLEAR Pin on 
#pragma CONFIG DEBUG = ON;	//Enable Debug Mode 
#pragma CONFIG LVP = OFF; 	//Low-Voltage programming disabled (necessary for debugging) 
#pragma CONFIG OSC = LP 	//Low pass oscillator 
#pragma CONFIG FCMEN = OFF	//Fail-safe clock monitor disabled
#pragma CONFIG IESO = OFF	//Oscillator swtchover mode disabled
#pragma CONFIG PWRT = ON	//PWRT enabled
#pragma CONFIG BOREN = OFF	//Brown-out reset dissabled in hardware and software
is this why the project was not working or is there any other problems aswell??
 

chrisw1990

Joined Oct 22, 2011
551
basically.. if your going to use the LDRs to trigger as digital voltage.. then your move.. but if you wanted to do it properly then you would use the ADC and compare the value.. and therefore measure the voltage if you like.. anyway, you say your in a rush.. one thing i will say though, if youre going to do it this way, youll have to get a multimeter and make sure the PIC is getting the right voltage for a logic level high from the LDR.. this may require changing your resistors with the LDRs.
:)
 

Thread Starter

harami

Joined Jun 20, 2011
66
basically.. if your going to use the LDRs to trigger as digital voltage.. then your move.. but if you wanted to do it properly then you would use the ADC and compare the value.. and therefore measure the voltage if you like.. anyway, you say your in a rush.. one thing i will say though, if youre going to do it this way, youll have to get a multimeter and make sure the PIC is getting the right voltage for a logic level high from the LDR.. this may require changing your resistors with the LDRs.
:)
i changed the resistor from 10k to 1.5k and i got these voltage feed into the pic:
1.6V when the ldr is bright and 3.8V when the ldr is dark however nothing happens. :(
 

chrisw1990

Joined Oct 22, 2011
551
thats because your voltage isnt triggering an input low.
implement the ADC. and compare ADC readings. this is the best way to do it. and you will get results.
in addition. i do not appreciate a sly message (with read confirmation on..) basically asking me to do your project.
 
Top