understanding the code

Discussion in 'Programmer's Corner' started by RG23, Jul 8, 2015.

  1. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    Code (C):
    1.  
    2. #defineF_CPU1000000UL  //definition for util/delay.h
    3.  
    4.  
    5. #include<avr/io.h>
    6.  
    7. #include<avr/wdt.h>
    8.  
    9. #include<avr/interrupt.h>
    10.  
    11. #include<util/delay.h>
    12.  
    13. #include<stdbool.h>
    14.  
    15.  
    16.  
    17.  
    18. #defineRELAYPB1  // Define RELAY ext output pin on PB1
    19.  
    20.  
    21.  
    22. #defineRELAY_OFF  PORTB|=(1<<RELAY)  //Define Relay off
    23.  
    24.  
    25.  
    26. #defineRELAY_ON  PORTB&=~(1<<RELAY)  //Define Relay on
    27.  
    28.  
    29.  
    30.  
    31.  
    32. unsignedcharrelay_change_state_level_Twist  =100;  //level for light on (NC-relay coil power off) for twist lock photo cell
    33.  
    34. unsignedcharrelay_change_state_level_btn  =100;  //level for light on (NC-relay coil power off) for btn photocell
    35.  
    36.  
    37.  
    38. unsignedcharrelay_change_state_level  =100;  //level for light on (NC-relay coil power off)
    39.  
    40. unsignedint  timer_period=58594;    // Timer period 58594 = 60s
    41.  
    42. unsignedint  timer_count=0;  // Variable increased each timer period
    43.  
    44.  
    45. unsignedchartimer_count_max=1;  //max time for counting //3 min
    46.  
    47. unsignedcharadc_level=0;  //current ADC level
    48.  
    49.  
    50.  
    51.  
    52.  
    53. bool  twistlock  =true;  //twistlock - true btn photocell -false
    54.  
    55.  
    56.  
    57. bool  adc_level_on=false;    //temp for on
    58.  
    59. bool  adc_level_on_prev=false;    //previous temp for on
    60.  
    61.  
    62. /*
    63.  
    64. bool  adc_level_off;//temp for off
    65.  
    66. */
    67.  
    68.  
    69.  
    70.  
    71.  
    72.  
    73.  
    74. /************************************************************************/
    75.  
    76. /* Timer initialization   */
    77.  
    78. /************************************************************************/
    79.  
    80.  
    81. voidtimer0_init()
    82.  
    83. {
    84.  
    85.   //TCCR0B |= (0<<CS01) | (1<<CS00);  // no pre-scaler
    86.  
    87.   TCCR0B|=(1<<CS02)|(0<<CS01)|(1<<CS00);  // pre-scaler 1024
    88.  
    89.   TIMSK0|=(1<<OCIE0A);  // enable timer comp interrupt
    90.  
    91.   TCCR0A|=(0<<WGM01)|(0<<WGM00);
    92.  
    93.   TCCR0B|=(0<<WGM03)|(1<<WGM02);
    94.  
    95.  
    96.  
    97.   OCR0A=timer_period;
    98.  
    99. }
    100.  
    101.  
    102.  
    103.  
    104. /************************************************************************/
    105.  
    106. /* ADC setup  */
    107.  
    108. /************************************************************************/
    109.  
    110.  
    111.  
    112. voidadc_setup(void)
    113.  
    114. {
    115.  
    116.   // Set the ADC input to PB2/ADC2
    117.  
    118.   ADMUX  |=(1<<MUX1)|(0<<MUX0);
    119.  
    120.   ADCSRA|=(1<<ADPS1)|(1<<ADPS0)|(1<<ADEN);
    121.  
    122. }
    123.  
    124.  
    125.  
    126. /************************************************************************/
    127.  
    128. /*  ADC read function  */
    129.  
    130. /************************************************************************/
    131.  
    132. unsignedcharadc_read(void)
    133.  
    134. {
    135.  
    136.   // Start the conversion
    137.  
    138.   ADCSRA|=(1<<ADSC);
    139.  
    140.  
    141.  
    142.   // Wait for it to finish
    143.  
    144.   while(ADCSRA&(1<<ADSC));
    145.  
    146.  
    147.  
    148.   returnADCL;
    149.  
    150.  
    151.  
    152. }
    153.  
    154.  
    155.  
    156.  
    157.  
    158. /************************************************************************/
    159.  
    160. /* main function  */
    161.  
    162. /************************************************************************/
    163.  
    164.  
    165. intmain(void)
    166.  
    167. {
    168.  
    169.  
    170.  
    171.   /************************************************************************/
    172.  
    173.   /* Local variable  */
    174.  
    175.   /************************************************************************/
    176.  
    177.  
    178.  
    179.  
    180.  
    181.  
    182.   /************************************************************************/
    183.  
    184.   /*  Clock setup  */
    185.  
    186.   /************************************************************************/
    187.  
    188.  
    189.  
    190.   // 1Mhz clock setup
    191.  
    192.  
    193.  
    194.   CCP  =0Xd8;
    195.  
    196.   CLKMSR=0b00;
    197.  
    198.   CCP  =0Xd8;
    199.  
    200.   CLKPSR=0x3;
    201.  
    202.  
    203.  
    204.  
    205.  
    206.  
    207.   /************************************************************************/
    208.  
    209.   /* Watchdog Timer setup  */
    210.  
    211.   /************************************************************************/
    212.  
    213.   WDTO_1S;  //WDT time 1c
    214.  
    215.  
    216.   /************************************************************************/
    217.  
    218.   /* Ports Initialization  */
    219.  
    220.   /************************************************************************/
    221.  
    222.  
    223.  
    224.  
    225.   DDRB|=(1<<RELAY);    // Set output direction on RELAY
    226.  
    227.   DDRB|=(0<<PB0);  // Set input direction on tpidata
    228.  
    229.  
    230.  
    231.   PUEB|=(1<<PUEB0);  //Enable PullUp port b0
    232.  
    233.  
    234.  
    235.  
    236.  
    237.  
    238.   /************************************************************************/
    239.  
    240.   /* Variable Initialization  */
    241.  
    242.   /************************************************************************/
    243.  
    244.  
    245.  
    246.   timer_count=0;
    247.  
    248.  
    249.  
    250.   /************************************************************************/
    251.  
    252.   /* Global Initialization  */
    253.  
    254.   /************************************************************************/
    255.  
    256.   cli();  //Disable all interrupts
    257.  
    258.   timer0_init();  // initialize timer
    259.  
    260.   adc_setup();
    261.  
    262.   sei();  //enable interrupts
    263.  
    264.  
    265.  
    266.   /************************************************************************/
    267.  
    268.   /* Main cycle  */
    269.  
    270.   /************************************************************************/
    271.  
    272.   while(1)
    273.  
    274.   {
    275.  
    276.   wdt_reset();
    277.  
    278.  
    279.  
    280.   twistlock=PB0;    //read twist lock - btn pin state
    281.  
    282.  
    283.  
    284.   if(twistlock)    // if twistlock (high)
    285.  
    286.   {
    287.  
    288.   relay_change_state_level=relay_change_state_level_Twist;  // set light level for twistlock
    289.  
    290.   }
    291.  
    292.   else
    293.  
    294.   {
    295.  
    296.   relay_change_state_level=relay_change_state_level_btn;  //or set light level for BTN photocell
    297.  
    298.   }
    299.  
    300.  
    301.  
    302.  
    303.  
    304.  
    305.  
    306.  
    307.  
    308.   if(timer_count<timer_count_max)
    309.  
    310.   {
    311.  
    312.   adc_level=adc_read();
    313.  
    314.  
    315.   adc_level_on_prev=adc_level_on;
    316.  
    317.  
    318.  
    319.  
    320.  
    321.   if(adc_level<=relay_change_state_level)
    322.  
    323.   {
    324.  
    325.   adc_level_on=true;
    326.  
    327.   adc_level_on_prev=adc_level_on;
    328.  
    329.   timer_count=timer_count_max;
    330.  
    331.   wdt_reset();
    332.  
    333.  
    334.  
    335.   }
    336.  
    337.   else
    338.  
    339.   {
    340.  
    341.   adc_level_on=false;
    342.  
    343.   wdt_reset();
    344.  
    345.  
    346.  
    347.   }
    348.  
    349.  
    350.  
    351.  
    352.  
    353.  
    354.  
    355.   if(adc_level_on_prev!=adc_level_on)
    356.  
    357.   {
    358.  
    359.   timer_count=0;
    360.  
    361.   }
    362.  
    363.  
    364.  
    365.   }
    366.  
    367.   else
    368.  
    369.   {
    370.  
    371.   if(adc_level_on)
    372.  
    373.   {
    374.  
    375.   RELAY_ON;
    376.  
    377.  
    378.  
    379.   timer_count=0;
    380.  
    381.   }
    382.  
    383.   else
    384.  
    385.   {
    386.  
    387.   RELAY_OFF;
    388.  
    389.  
    390.  
    391.   timer_count=0;
    392.  
    393.   }
    394.  
    395.   }
    396.  
    397.  
    398.  
    399.   }
    400.  
    401. }
    402.  
    403.  
    404. /************************************************************************/
    405.  
    406. /* Timer 0 interrupt vector  */
    407.  
    408. /************************************************************************/
    409.  
    410.  
    411. ISR(TIM0_COMPA_vect)
    412.  
    413. {
    414.  
    415.  
    416.   timer_count++;
    417.  
    418.  
    419. }
    420.  


    I need help in understanding the code above and make modifications to change the timer setttings.

    Thanks
     
    Last edited by a moderator: Jul 8, 2015
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    11,924
    2,971
    It is easier to read if you used the CODE tags.
    Max.
    Code (Text):
    1. #defineF_CPU1000000UL //definition for util/delay.h
    2.  
    3.  
    4. #include<avr/io.h>
    5. #include<avr/wdt.h>
    6. #include<avr/interrupt.h>
    7. #include<util/delay.h>
    8. #include<stdbool.h>
    9.  
    10. #defineRELAYPB1 // Define RELAY ext output pin on PB1
    11.  
    12. #defineRELAY_OFF PORTB|=(1<<RELAY) //Define Relay off
    13.  
     
  3. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,382
    1,168
    There seems to be something with the code tags, as a problem appears in both preceding posts. Spaces seem to have disappeared. Could this have anything to to with recent changes, @jrap?
     
  4. bertus

    Administrator

    Apr 5, 2008
    16,433
    2,696
  5. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    right now the output goes from low to high immediately whereas from high to low there is a delay of 1min.

    I need to add delay of about 1min for low to high transition as well.

    If anyone has an idea, let me know
    thanks
     
  6. theonewho

    New Member

    Jul 9, 2015
    17
    2
    Where did this code come from and why are there no spaces where there should be to make this valid code?

    Code (C):
    1. #defineRELAYPB1  // Define RELAY ext output pin on PB1
    2. #defineRELAY_OFF  PORTB|=(1<<RELAY)  //Define Relay off
    3. #defineRELAY_ON  PORTB&=~(1<<RELAY)  //Define Relay on
    4.  
    5. unsignedcharrelay_change_state_level_Twist  =100;  //level for light on (NC-relay coil power off) for twist lock photo cell
    6. unsignedcharrelay_change_state_level_btn  =100;  //level for light on (NC-relay coil power off) for btn photocell
    Code (C):
    1. unsignedcharadc_read(void)
    2. {
    3.   // Start the conversion
    4.   ADCSRA |= (1 << ADSC);
    5.  
    6.   // Wait for it to finish
    7.   while (ADCSRA & (1 << ADSC));
    8.  
    9.   returnADCL;
    10.  
    11. }
    all those blank lines...
     
  7. WBahn

    Moderator

    Mar 31, 2012
    19,129
    5,172
    Instead of asking people to wade through 420 lines of improperly formatted code that is spottily commented, how about spending some time up front to make the code easily readable and then indicating where in the code the problem seems to be originating (as best as you can tell)?

    Code (C):
    1.  
    2. #define F_CPU 1000000UL  //definition for util/delay.h
    3.  
    4. #include <avr/io.h>
    5. #include <avr/wdt.h>
    6. #include <avr/interrupt.h>
    7. #include <util/delay.h>
    8. #include <stdbool.h>
    9.  
    10. #define RELAY PB1  // Define RELAY ext output pin on PB1
    11. #define RELAY_OFF  PORTB|=(1<<RELAY)  //Define Relay off
    12. #define RELAY_ON  PORTB&=~(1<<RELAY)  //Define Relay on
    13.  
    14. unsigned char relay_change_state_level_Twist  = 100;  //level for light on (NC-relay coil power off) for twist lock photo cell
    15. unsigned char relay_change_state_level_btn  = 100;  //level for light on (NC-relay coil power off) for btn photocell
    16. unsigned char relay_change_state_level  = 100;  //level for light on (NC-relay coil power off)
    17. unsigned int  timer_period = 58594;    // Timer period 58594 = 60s
    18. unsigned int  timer_count = 0;  // Variable increased each timer period
    19. unsigned char timer_count_max = 1;  //max time for counting //3 min
    20. unsigned char adc_level = 0;  //current ADC level
    21.  
    22. bool  twistlock  = true;  //twistlock - true btn photocell -false
    23. bool  adc_level_on = false;    //temp for on
    24. bool  adc_level_on_prev = false;    //previous temp for on
    25.  
    26. /*
    27. bool  adc_level_off;//temp for off
    28. */
    29.  
    30. /************************************************************************/
    31. /* Timer initialization   */
    32. /************************************************************************/
    33.  
    34. voidtimer0_init()
    35. {
    36.   //TCCR0B |= (0<<CS01) | (1<<CS00);  // no pre-scaler
    37.   TCCR0B |= (1<<CS02)|(0<<CS01)|(1<<CS00);  // pre-scaler 1024
    38.   TIMSK0 |= (1<<OCIE0A);  // enable timer comp interrupt
    39.   TCCR0A |= (0<<WGM01)|(0<<WGM00);
    40.   TCCR0B |= (0<<WGM03)|(1<<WGM02);
    41.   OCR0A = timer_period;
    42. }
    43.  
    44.  
    See how much more readable that is?

    You are much more likely to get someone to look through your code if you make some effort up front to make it easier for them to do so.
     
    theonewho likes this.
  8. ranch vermin

    Member

    May 20, 2015
    85
    2
    Wow I hadent ever seen binary hardware code in the flesh, that was cool - thanks for showing me.
     
Loading...