Attiny861 Duty Cycle Variation

Discussion in 'Embedded Systems and Microcontrollers' started by nudmat, Aug 6, 2014.

  1. nudmat

    Thread Starter New Member

    May 27, 2014
    4
    0
    Good day to all,
    Please i need your support.I am Controlling Led brightness with PWM Signal and at the same time Displaying the duty cycle(represented in %) on an Lcd Display.The Problem i am having is that the percentage of the duty cycle shown on the oscilloscope is different from the value shown on my Lcd Display.For example when the oscilloscope showed 50% duty cycle the value shown on the LCD was 38%.I would be glad if somebody could help me have a look at my code may be there is something i am not doing correctly.Below are my lines of code.Thank you for the usual Support.
    Best regards.

    Code ( (Unknown Language)):
    1.  
    2.  # define F_CPU 1000000UL
    3.  #include <avr/io.h>
    4. #include <avr/interrupt.h>
    5. #include <avr/delay.h>
    6. #include "NewHeaven_LCD.h"
    7.  #include "USI_TWI_Master.h"
    8.  #define SLAVE_ADDR  0x50
    9.  #define MESSAGEBUF_SIZE       20
    10.  #define TWI_GEN_CALL         0x00  // The General Call address is 0
    11.  // Sample TWI transmission commands
    12. //#define TWI_CMD_MASTER_WRITE 0x28
    13. //#define TWI_CMD_MASTER_READ  0x29
    14.  // Sample TWI transmission states, used in the main application.
    15. #define SEND_DATA             0x01
    16. #define REQUEST_DATA          0x02
    17. #define READ_DATA_FROM_BUFFER 0x03
    18. #define RTC_ADDRRESS          0x34 //0x68
    19. #define RTC_REG_POINTER       0x00
    20.  unsigned char messageBuf[MESSAGEBUF_SIZE];
    21. unsigned char TWI_DisplaySlaveAddress,TWI_RTCSlaveAddress, temp, pressedButton, myCounter=0;
    22.  unsigned char TWI_Act_On_Failure_In_Last_Transmission ( unsigned char TWIerrorMsg )
    23. {
    24.                     // A failure has occurred, use TWIerrorMsg to determine the nature of the failure
    25.                     // and take appropriate actions.
    26.                     // Se header file for a list of possible failures messages.
    27.    asm volatile ("nop");
    28.                      
    29.   return TWIerrorMsg;
    30. }
    31.  
    32. unsigned char Reset_RTC()
    33. {
    34.  messageBuf[0]=(TWI_RTCSlaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
    35.     messageBuf[1] = RTC_REG_POINTER;
    36.     messageBuf[2] = 0;
    37.  messageBuf[3] = 0;
    38.  messageBuf[4] = 0;
    39.  messageBuf[5] = 0;
    40.  messageBuf[6] = 0;
    41.  messageBuf[7] = 0;
    42.  USI_TWI_Start_Transceiver_With_Data(messageBuf,8);
    43.  
    44.  return 0;
    45. }
    46.  int main( void )
    47. {
    48.   char          time_array[10];//HH:MM:SS
    49.   unsigned char repeat=1;
    50.   unsigned char sec,min,hr;
    51.   unsigned char i,check=1;
    52.    
    53.  
    54.   unsigned char mode=0;  // mode=0 : Power_Setting    ;;;; mode=1 : Time_Setting
    55.   int LED_Power=1;
    56.   int On_Time=0;
    57.   int LED_on=0;
    58.   int Duration=0;
    59.   int RTC_Seconds=0;
    60.  
    61.   TWI_DisplaySlaveAddress   = 0x28;
    62.   TWI_RTCSlaveAddress       = 0x68;
    63.   sei();
    64.   USI_TWI_Master_Initialise();
    65.    
    66.    messageBuf[0]=(TWI_DisplaySlaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);  // Display an 0x28 schreibend ansprechen
    67.   messageBuf[1]=0xFE;
    68.   messageBuf[2]=0x53;
    69.   messageBuf[3]=8;
    70.    
    71.  
    72.   USI_TWI_Start_Transceiver_With_Data(messageBuf,4);
    73.    
    74.   // Set PWM-Port PB4
    75.   PORTB = 0x00;
    76.   DDRB   |=(1<<PB4) ;          //PWM output pin
    77.   TCCR1A |= ((1 << PWM1B));                 // Enable Fast PWM for OCR1B
    78.   TCCR1C |= ( (1 << PWM1D) );            // Enable Fast PWM for OCR1D
    79.   TCCR1C |= ( (1 << COM1D0) );           // OC1D & OC1D^ are connected,Clear on Compare Match
    80.   OCR1C   = 77;
    81.   OCR1D   = LED_Power;  
    82.   //Invert output
    83.   TCCR1B |= (1 << PWM1X);
    84.   // Start timer1
    85.   TCCR1B |= (1 << CS10)|(1 << CS11)|(1 << CS12);
    86.      
    87.   //Switch internal pullups on
    88.   PORTA |= (1<<PA7) | (1<<PA6) |(1<<PA5) |(1<<PA4);
    89.   PORTB |= (1<<PB6);
    90.    Clear_LCD();
    91.    
    92.   for(i=0;i<8;i++)
    93.  
    94.   {
    95.    
    96.    time_array[i]='0';
    97.   }
    98.  
    99.   time_array[2]=time_array[5]=':';
    100.  
    101.   /*Convert ASCII to BCD coded data and store in RTC registers*/
    102.  
    103.   for(i=0; i < 8; i++)
    104.  
    105.   {
    106.    if ((i==2) || (i==5))
    107.    
    108.    ;  //do nothing
    109.     else
    110.     {
    111.     time_array[i] -= 48;
    112.    }
    113.   }
    114.   sec = (((time_array[6] << 4) & 0xf0) | (time_array[7]));
    115.  min = (((time_array[3] << 4) & 0xf0) | (time_array[4]));
    116.  hr =  (((time_array[0] << 4) & 0xf0) | (time_array[1]));
    117.  
    118.    /*Write data (time ) in RTC*/
    119.    
    120.     messageBuf[0]=(TWI_RTCSlaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
    121.     messageBuf[1] = RTC_REG_POINTER;
    122.     messageBuf[2] = sec;
    123.  messageBuf[3] = min;
    124.  messageBuf[4] = hr;
    125.     USI_TWI_Start_Transceiver_With_Data(messageBuf,5);
    126.  
    127.   while(1==1)
    128.     {
    129.    
    130.   if (mode==0)  // Power_Setting
    131.   {
    132.    if (!(PINA & 64 ))
    133.    {
    134.     if (LED_Power>1) LED_Power--;
    135.    
    136.    }
    137.    
    138.    else if (!(PINA & 16 ))
    139.    {
    140.     if (LED_Power<100) LED_Power++;
    141.    }
    142.   }
    143.  
    144.   if (mode==1)  // Time_Setting
    145.   {
    146.    if (!(PINA & 64 ))
    147.    {
    148.     if (On_Time>0) On_Time--;
    149.    }
    150.    else if (!(PINA & 16 ))
    151.    {
    152.     if (On_Time<300) On_Time++;  // maximale On-time 5min
    153.    
    154.    }
    155.   }
    156.  
    157.   if (!(PINA & 32))
    158.   {
    159.    switch (mode)
    160.    {
    161.     case 0: mode=1;
    162.             break;
    163.     case 1: mode=2;
    164.             break;
    165.     default:mode=0;
    166.             break;
    167.    }
    168.    
    169.    
    170.    _delay_ms(200);
    171.   }  
    172.    
    173.  
    174.   if (mode==0)
    175.   {   SetPos_LCD(0,0);
    176.    sprintf(lcd_text,"*Power One %d   ",LED_Power);
    177.    WriteString_LCD();
    178.       SetPos_LCD(0,1);
    179.    if (On_Time) sprintf(lcd_text," Real Time %ds   ",Duration); else sprintf(lcd_text," On_Time Period   ");
    180.    WriteString_LCD();
    181.    _delay_ms(10);
    182.   }
    183.  
    184.   if (mode==1)
    185.   {   SetPos_LCD(0,0);
    186.    sprintf(lcd_text," Power One %d  ",LED_Power);
    187.    WriteString_LCD();
    188.    SetPos_LCD(0,1);
    189.    if (On_Time) sprintf(lcd_text,"*Time One %ds   ",Duration); else sprintf(lcd_text,"*On_Time Period  ");
    190.    WriteString_LCD();
    191.    _delay_ms(10);
    192.   }
    193.    
    194.   if (!(PINA & 128))
    195.    {
    196.      _delay_ms(200);
    197.      if (LED_on==0) {LED_on=1; Reset_RTC(); RTC_Seconds=0;} else LED_on=0;
    198.      if (LED_on) {OCR1D=50;  _delay_ms(1);}  
    199.    }
    200.    
    201.   if (LED_on) OCR1D=LED_Power; else OCR1D=0;
    202.  
    203.   if (LED_on==0)
    204.   {
    205.    Duration=On_Time;
    206.    SetPos_LCD(15,0);
    207.    sprintf(lcd_text," ");
    208.    WriteString_LCD();
    209.   }
    210.   else
    211.   {
    212.    SetPos_LCD(15,0);
    213.    sprintf(lcd_text,"*");
    214.    WriteString_LCD();
    215.   }  
    216.   if ((LED_on) && (On_Time))
    217.   {
    218.    if (RTC_Seconds>=On_Time)
    219.     LED_on=0;
    220.    else
    221.    Duration=On_Time-RTC_Seconds;    
    222.   }    
    223.      
    224.      
    225.       // Set RTC Register
    226.       messageBuf[0]=(TWI_RTCSlaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
    227.       messageBuf[1] = RTC_REG_POINTER;
    228.       USI_TWI_Start_Transceiver_With_Data(messageBuf,2);
    229.      
    230.       // Read RTC Register
    231.       messageBuf[0]=(TWI_RTCSlaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_READ_BIT);
    232.       USI_TWI_Start_Transceiver_With_Data(messageBuf,2);
    233.       sec=messageBuf[1];
    234.      
    235.      
    236.       // Set RTC Register
    237.       messageBuf[0]=(TWI_RTCSlaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
    238.       messageBuf[1] = 0x01;
    239.       USI_TWI_Start_Transceiver_With_Data(messageBuf,2);
    240.      
    241.       // Read RTC Register
    242.       messageBuf[0]=(TWI_RTCSlaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_READ_BIT);
    243.       USI_TWI_Start_Transceiver_With_Data(messageBuf,2);
    244.       min=messageBuf[1];
    245.      
    246.      
    247.       // Set RTC Register
    248.       messageBuf[0]=(TWI_RTCSlaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
    249.       messageBuf[1] = 0x02;
    250.       USI_TWI_Start_Transceiver_With_Data(messageBuf,2);
    251.      
    252.       // Read RTC Register
    253.       messageBuf[0]=(TWI_RTCSlaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_READ_BIT);
    254.       USI_TWI_Start_Transceiver_With_Data(messageBuf,2);
    255.       hr=messageBuf[1];
    256.      
    257.       // convert time to seconds
    258.    RTC_Seconds=   (sec & 0x0f) + 10* ((sec >> 4) & 0x0f)  // seconds
    259.                + 60*(min & 0x0f) + 600*((min >> 4) & 0x0f); // minutes to secs
    260.       // convert BCD coded decimal to ASCII code, display on LCD
    261.      
    262.       time_array[0] = (hr >> 4) & 0x0f;
    263.       time_array[1] = hr & 0x0f;
    264.       time_array[3] = (min >> 4) & 0x0f;
    265.       time_array[4] = min & 0x0f;
    266.       time_array[6] = (sec >> 4) & 0x0f;
    267.       time_array[7] = sec & 0x0f;
    268.      
    269.      
    270.       for(i=0; i< 8; i++)
    271.       {
    272.        if ((i==2) || (i==5))
    273.        ;  //do nothing
    274.        else
    275.        {
    276.          time_array[i] += 48;
    277.        }
    278.       }
    279.      
    280.       time_array[8]=0;
    281.      
    282.    
    283.       //SetPos_LCD(0,1);
    284.       //sprintf(lcd_text,"Second %s   ",time_array);
    285.    //sprintf(lcd_text,"seconds %d   ",RTC_Seconds);
    286.       //WriteString_LCD();
    287.      
    288.      
    289.  
    290.              
    291.   //    OCR1B=64;    
    292.        
    293.    }
    294.    
    295. }
    296.  
    297.  
    298. [/i][/i][/i]
     
Loading...