help with code and hardware PIC18F452

Discussion in 'The Projects Forum' started by InnocentOfTheWorld, Mar 21, 2013.

  1. InnocentOfTheWorld

    Thread Starter Member

    Apr 15, 2010
    47
    1
    I have two switches attached to pic18f452
    one is on PORTA pin 0 other on PORTA pin 1.
    I want to monitor timing for how much long they are on (both , single and none).
    LCD is attached to show count of the loop so i can calculate time with the help of machine cycle.

    I tried first only to send a character after recording values of the events(switch on off or both ) four times and then averaging it.

    Both i need help on proteus circuit and code. Kindly tell me the errors.
     
  2. absf

    Senior Member

    Dec 29, 2010
    1,492
    372
    Unable to open your .DSN file as my version of ISIS is older than yours.

    Anyway I am posting your code here so others dont need to unzip.

    Code ( (Unknown Language)):
    1. #include <p18f452.h>
    2. #define heel PORTAbits.RA0    // Sensor input (Limit switch at heel)
    3. #define toe PORTAbits.RA1     // Sensor input (Limit switch at toe)
    4. #define ldata PORTD           // PORT D = LCD data pins
    5. #define rs PORTBbits.RB0      // rs = PORTB.0
    6. #define rw PORTBbits.RB1      // rw = PORTB.1
    7. #define en PORTBbits.RB2      // en = PORTB.2
    8. #define busy PORTDbits.RD7    // busy = PORTD.7
    9. unsigned int dorsiflexion = 0;
    10. unsigned int plantarflexion = 0;
    11. unsigned int swing =0;
    12. void LCD_data( unsigned int ,unsigned int,unsigned int);
    13. void lcdcmd(unsigned char);
    14. void lcddata(unsigned char);
    15. void lcdready(void);
    16. void MSDelay (unsigned int );
    17.  
    18. void main(void)
    19. {
    20.  int cycle = 0;
    21.  int change=1;
    22.  TRISA = 0xff ;
    23.  TRISD = 0;
    24.  TRISB = 0;
    25.  PORTB = 0;
    26.  en = 0;
    27.  
    28. while(cycle!=5)
    29. {
    30.  
    31. if(heel==1 & toe==1)
    32.    {
    33.       PORTBbits.RB3 = 1 ;
    34.       if(change==1)
    35.         {  
    36.            
    37.             cycle++;
    38.             change=0;
    39.          
    40.         }
    41.     }  
    42. else if(cycle==5)
    43.     LCD_data(swing , plantarflexion, dorsiflexion);  
    44. else if(heel==0 && toe==1)
    45.       {
    46.          plantarflexion++;
    47.       }
    48. else if (heel==0 && toe==0)
    49.       {
    50.          swing++;
    51.       }
    52. else if (heel==1 && toe==0)
    53.       {  
    54.          dorsiflexion++;
    55.          change=1;
    56.       }
    57. }
    58. }
    59. void LCD_data(unsigned int a,unsigned int b,unsigned int c)
    60.     {
    61.         unsigned int d,e,f;
    62.         unsigned char h[2];
    63.         unsigned char i[2];
    64.         unsigned char j[2];
    65.         d=a/4; e=b/4; f=c/4;
    66.         h[0]= d & 0xff;
    67.         h[1]=(d>>8)& 0xff;
    68.         i[0]= e & 0xff;
    69.         i[1]=(e>>8)& 0xff;
    70.         j[0]= f & 0xff;
    71.         j[1]=(f>>8)& 0xff;
    72.         MSDelay(250);
    73.         lcdcmd(0x38);
    74.         MSDelay(250);
    75.         lcdcmd(0x0E);
    76.         lcdready();
    77.         lcdcmd(0x01);
    78.         lcdready();
    79.         lcdcmd(0x06);
    80.         lcdready();
    81.         lcdcmd(0x86);
    82.         lcdready();
    83.         lcddata('a');
    84. }
    85.  
    86. void lcdcmd(unsigned char value  )
    87.  
    88. {
    89.     ldata = value;
    90.     rs = 0;
    91.     rw = 0;
    92.     en = 1;
    93.     MSDelay (1);
    94.     en = 0;
    95. }
    96.  
    97. void lcddata(unsigned char value  )
    98.  
    99. {
    100.     ldata = value;
    101.     rs = 1;
    102.     rw = 0;
    103.     en = 1;
    104.     MSDelay (1);
    105.     en = 0;
    106. }
    107.  
    108. void lcdready()
    109. {
    110.     TRISD= 0xff;
    111.     rs = 0 ;
    112.     rw = 1;
    113.     do
    114.         {
    115.             en = 1;
    116.             MSDelay(1);
    117.             en = 0;
    118.         } while (busy==1);
    119.         TRISD=0;
    120.  
    121.  
    122.  
    123. }
    124.  
    125. void MSDelay(unsigned int itime)
    126.  
    127. {
    128.     unsigned int k,l;
    129.     for(k=0;k<itime;k++)
    130.     for(l=0;l<135;l++);
    131.  
    132. }
    I think you forgot to mention what is the problem that you have encountered. Hardware or software ?

    Allen
     
    Last edited: Mar 21, 2013
  3. InnocentOfTheWorld

    Thread Starter Member

    Apr 15, 2010
    47
    1
    consider this simple code.

    #include <p18f452.h>
    #define heel PORTAbits.RA0 // Sensor input (Limit switch at heel)
    #define toe PORTAbits.RA1 // Sensor input (Limit switch at toe)
    #define ldata PORTB // PORT D = LCD data pins

    void delay_s(int);

    void main(void)
    {
    int cycle = 0;
    int change=1;
    TRISA = 3 ;
    TRISB = 0;
    PORTAbits.RA6=0;

    while(1)
    {

    if(PORTA==3)
    {
    ldata=255;
    delay_s(250);
    }
    else
    { ldata=0;
    delay_s(250);
    }
    }
    }
    void delay_s(int itime)
    {
    unsigned int k,l;
    for(k=0;k<itime;k++)
    for(l=0;l<135;l++);

    }

    i make both pins of PORTA 0 and 1 as input and set them high.
    as in my code all of PORTB should get high but that is not happening in Proteus they all are low. Is my code not ok?
    In simple my problem is that controller is not reading input from its pins.
     
    Last edited: Mar 21, 2013
  4. absf

    Senior Member

    Dec 29, 2010
    1,492
    372
    Port A is an 8 bit port. How are you sure that RA2-RA7 are all zeros?
    I think you should mask (logical AND) portA with 03H first before testing to be safe.

    Are PortA set to analogue by default in 452? In the 16F series you have to set the input to digital before using it.

    Allen
     
    Last edited: Mar 21, 2013
  5. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,043
    3,807
    Yes, clear ANCON0 register, then set TRISA to 0x03

    No need to set input pins high, they don't respond - they only measure the voltage connected to them from the circuit.
     
Loading...