is there something wrong with the code?

Discussion in 'Embedded Systems and Microcontrollers' started by rayda, Apr 23, 2011.

  1. rayda

    Thread Starter New Member

    Apr 23, 2011
    3
    0
    is there something wrong with this code? This is for PIC16f886 where the project will work depending on the LDR and temperature sensor in the hardware part.
    Code ( (Unknown Language)):
    1. #include <pic.h>
    2.  
    3. __CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & DUNPROTECT & \
    4. BORDIS & IESOEN & FCMDIS & LVPDIS & DEBUGEN);
    5. __CONFIG(BORV21);
    6.  
    7. #define FOSC 8000000L
    8.  
    9. #define LDR_THRESHOLD 50
    10. #define MAX_DCYCLE 255
    11.  
    12. const char SSEG[] = {
    13. 0b11000000,
    14. 0b11111001,
    15. 0b10100100,
    16. 0b10110000,
    17. 0b10011001,
    18. 0b10010010,
    19. 0b10000010,
    20. 0b11111000,
    21. 0b10000000,
    22. 0b10010000,
    23. 0b11000110,
    24. 0b10001110
    25. };
    26.  
    27. unsigned char DispDigit[4];
    28. unsigned char DigitCount;
    29. unsigned char TempType;
    30.  
    31. static void interrupt isr(void)
    32. {
    33.     if(T0IF){
    34.         PORTC = DispDigit[DigitCount];
    35.  
    36.         PORTB = ~(1 << DigitCount++);
    37.        
    38.         if(DigitCount >3)
    39.             DigitCount = 0;
    40.  
    41.         TMR0 = 156;
    42.         T0IF = 0;
    43.     }
    44. }
    45.  
    46. #define _delay_us(x) { unsigned char us; \
    47.                us = (x)/(12000000/FOSC)|1; \
    48.                while(--us != 0) continue; }
    49.  
    50.  
    51. void _delay_ms(unsigned int ms)
    52. {
    53.     unsigned char i;
    54.     do {
    55.         i =4;
    56.         do {
    57.             _delay_us(164);
    58.         }while(--i);
    59.     }while(--ms);
    60. }
    61.  
    62. void SSEG_putnum(float number)
    63. {
    64.     unsigned char iDigit, iDigit1, iDecimal;
    65.  
    66.     if(number > 99.9) return;
    67.    
    68.     GIE = 0;
    69.  
    70.     iDigit = number;
    71.     iDecimal = (number - iDigit) *10;
    72.     DispDigit[1] = SSEG[iDecimal];
    73.  
    74.     if(iDigit >= 10) {
    75.         iDigit1 = iDigit / 10;
    76.         DispDigit[3] = SSEG[iDigit1];
    77.         iDigit = iDigit - (iDigit1 *10);
    78.     } else {
    79.         DispDigit[3] = SSEG[0];
    80.     }
    81.     DispDigit[2] = SSEG[iDigit] & 0x7F;
    82.    
    83.     GIE = 1;
    84. }
    85.  
    86. void main (void)
    87. {
    88.     unsigned int iValue, iCTemp;
    89.     unsigned char ldr_value;
    90.     float CentTemp;
    91.  
    92.     OSCCON=0x70;        
    93.  
    94.     TRISA = 0xFF;        
    95.     TRISB = 0x00;      
    96.     TRISC = 0x00;        
    97.     ANSEL = 0b00000011;  
    98.     ANSELH = 0b00000000;
    99.  
    100.     PORTC=0xFF;
    101.     PORTB=0xFF;
    102.  
    103.     OPTION = 0b00000101;
    104.     TMR0=156;            
    105.     T0IE = 1;          
    106.     GIE = 1;         
    107.  
    108.     CCP1CON=0b00001100;  
    109.     CCPR1L=MAX_DCYCLE;  
    110.    
    111.    
    112.     T2CON=0b00000101;    
    113.     PR2=0x65;            
    114.     TMR2=0;              
    115.     PSTRCON=0b00001000;  
    116.  
    117.     DigitCount=0;
    118.     TempType=0;              
    119.     DispDigit[0]=SSEG[10];    
    120.     DispDigit[1]=SSEG[0];      
    121.     DispDigit[2]=SSEG[0];      
    122.     DispDigit[3]=SSEG[0];      
    123.  
    124.     for(;;){
    125.         ADCON0=0b11000001;      
    126.         ADCON1=0b10110000;      
    127.         GODONE=1;                
    128.         while(GODONE) continue;
    129.  
    130.         iValue=ADRESL;          
    131.         iValue += (ADRESH << 8);
    132.         iCTemp = iValue;
    133.         _delay_ms(50);
    134.  
    135.         GODONE=1;  
    136.         while(GODONE) continue;
    137.  
    138.         iValue=ADRESL;          
    139.         iValue += (ADRESH << 8);
    140.         iCTemp += iValue;
    141.         _delay_ms(50);
    142.  
    143.         GODONE=1;  
    144.         while(GODONE) continue;  
    145.        
    146.         iValue=ADRESL;          
    147.         iValue += (ADRESH << 8);
    148.         iCTemp += iValue;
    149.  
    150.         CentTemp=(iCTemp/3.0)/ 10.24;
    151.        
    152.         ADCON0=0b11000101;      
    153.         ADCON1=0b00000000;      
    154.         GODONE=1;                
    155.         while(GODONE) continue;
    156.  
    157.         ldr_value = ADRESH;  
    158.        
    159.         if (ldr_value > LDR_THRESHOLD)
    160.      ldr_value = LDR_THRESHOLD;
    161.        
    162.         CCPR1L=MAX_DCYCLE - (5 * ldr_value);
    163.  
    164.         if (RA4 == 0) {            
    165.             _delay_ms(1);
    166.             if (RA4 == 0) {            
    167.             TempType=~TempType;      
    168.             }
    169.         }
    170.  
    171.     if (TempType) {
    172.       CentTemp=((9/5) * CentTemp) + 32;
    173.       DispDigit[0]=SSEG[11];     // Fahrenheit Sign
    174.     } else {
    175.       DispDigit[0]=SSEG[10];     // Centigrade Sign
    176.     }
    177.  
    178.     SSEG_putnum(CentTemp);
    179.     _delay_ms(200);
    180.   }
    181. }
    Please, anyone, please help me to look at it.
    I would need to confirm that nothing wrong with the code.
    Thank you.
     
  2. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    What compiler are you using?
     
  3. rayda

    Thread Starter New Member

    Apr 23, 2011
    3
    0
    I used the Hi-tech C compiler of MPLAB IDE.
     
  4. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    One point that strike me is that you global variable DigitCount should be declared as "volatile unsigned char".
    Any global variable that is used in an interrupt routine should be declared volatile to make sure that its value is read each time before it is used.
     
Loading...