-->

PWM sinewave generation Using for pic16f877a

By
To generate a PWM sine wave using a PIC16F877A microcontroller, you can use the following steps: 1)Configure the Timer2 module of the PIC16F877A microcontroller to generate a PWM signal. The PWM frequency can be set by configuring the PR2 register and the duty cycle can be set by configuring the CCPR1L and CCP1CON registers. 2)Generate a sine wave lookup table using a spreadsheet software or an online tool. The lookup table should contain the sine values for one cycle of the wave, ranging from 0 to 2π. 3)Store the sine wave lookup table in the program memory of the microcontroller. 4) Set up a Timer1 module of the PIC16F877A microcontroller to generate interrupts at a frequency that is equal to the desired frequency of the sine wave. In the interrupt service routine (ISR) for Timer1, read the values from the sine wave lookup table and update the duty cycle of the PWM signal generated by Timer2.
       

            #include 

// sine wave lookup table
const unsigned char sine_table[64] = {128,140,152,163,174,185,195,205,214,222,230,237,243,248,252,255,
                                      255,255,252,248,243,237,230,222,214,205,195,185,174,163,152,140,
                                      128,116,104,93,82,71,61,51,42,34,26,19,13,8,4,0,
                                      0,0,4,8,13,19,26,34,42,51,61,71,82,93,104,116};

// timer1 ISR
void __interrupt() timer1_isr(void) {
    static unsigned char table_index = 0;
    
    // update duty cycle of PWM signal generated by timer2
    CCPR1L = sine_table[table_index];
    
    // increment table index
    table_index++;
    if (table_index >= 64) {
        table_index = 0;
    }
}

void main(void) {
    // configure timer2 for PWM generation
    T2CON = 0b00000111; // prescaler = 1, postscaler = 1, timer2 on
    PR2 = 255; // set PWM frequency
    CCP1CON = 0b00001100; // PWM mode, PWM duty cycle controlled by CCPR1L
    
    // configure timer1 for interrupt generation
    T1CON = 0b00110101; // prescaler = 8, timer1 on, enable timer1 interrupt
    TMR1IF = 0; // clear timer1 interrupt flag
    TMR1H = 0xFC; // set timer1 reload value for 50 Hz interrupt frequency
    TMR1L = 0x18;
    PEIE = 1; // enable peripheral interrupts
    GIE = 1; // enable global interrupts
    
    while (1) {
        // main program loop
    }
}

       
 
In this example code, the sine wave lookup table contains 64 values, so the Timer1 module is configured to generate interrupts at a frequency of 50 Hz (the table is updated once per cycle). The Timer2 module is configured to generate a PWM signal with a frequency of 8 kHz. The duty cycle of the PWM signal is updated in the Timer1 ISR by reading the values from the sine wave lookup table.

dsPIC33 PWM Controlls iin accordance with ADC value

By
              dsPIC33 PWM Controlls iin accordance with ADC value 




dsPIC33 PWM Controlls iin accordance with ADC value

By
              dsPIC33 PWM Controlls iin accordance with ADC value 




Attemt to Access Keyboard

By


 * main.c
 *
 *  Created on: Aug 12, 2017
 *      Author: thannara123
 



#include
#define FALSE 0
#define TRUE  1  // 0r //!FALSE
struct menu
 {
   char menu_up_key;
   char menu_down_key;
   char menu_enter_key :1; // bit feild 1 bit
 };
struct menu s1= {0}; //s1.menu_up-key =1;
void struct_1( struct menu,const char *menu_display[]);



int main()
{

     const char *menu_display[10];
      menu_display[0] = "Menu0";
      menu_display[1] = "Menu1";
      menu_display[2] = "Menu2";
      menu_display[3] = "Menu3";
      menu_display[4] = "Menu4";
      menu_display[5] = "menu 5";
      menu_display[6] = "menu 6";


       struct_1(s1,menu_display);
             getchar();
             return 0;

      }


void struct_1(struct menu s1 ,const char *menu_display[])
{

     char ch;
      do{
            ch=getch();


            if(ch!='\0')
            {
              ch=getch();
              if(ch=='H')
              {
               s1.menu_up_key++;
               if(s1.menu_up_key==7)
                 s1.menu_up_key =0;
            // printf("UP\n");
                  printf(" %s\n",*(s1.menu_up_key+menu_display));
               }
         else    if(ch=='P')
               {
              s1.menu_up_key--;
              if(s1.menu_up_key==-1)
                  {  s1.menu_up_key =6;
                   goto here;
                  }
                //printf("down\n");

                  here:
         printf("%s\n",*(s1.menu_up_key+menu_display));


                   }

            }
               }
              while(ch!='e');// while oka key
}
/*
 * button_key.c
 *
 * Created: 7/30/2017 12:02:39 PM
 *  Author: Krishna
 */ #include 
 #include "delay.h"
  #include "button_key.h"
  #include "lcd.h"  
    
void menu_key_display(struct menu s1,const char *menu_display[]);
void UP_Down_Keyvalue(struct menu s1,int i,int j);

 /* Function Key Value For get key  */
int Key_pressed(void)
 { 
 while(1){
    if (LEFT_S) {  while(LEFT_S);return 1; }      
 if (RIGHT_S){  while(RIGHT_S);return 2; }
 if (UP_S)  {  while(UP_S); return 3; }
 if (DOWN_S) { while(DOWN_S);return 4 ; }
 if (OK_S) { while(OK_S);return 5 ; }
 } 
 } 
 
 
 /* Function Key Value For Up Key  & Enter*/
 
 void menu_key_display(struct menu s1,const char *menu_display[])
 {   
  int ch; 
   int a; 
  int  menu_position =0;
   LCD_DisplayString(menu_display[menu_position]);
            do{  
    
    repat:          
    ch = Key_pressed();
    if(ch==1||ch==2)
    {
                  if(ch==2)
                    {  if(ch==2)
                   { if(s1.menu_side_key==4)
                     s1.menu_side_key = 0;
                  LCD_Clear();
                  LCD_GoToLine(0);
                              LCD_DisplayString(*((++s1.menu_side_key)+menu_display)); 
         menu_position=1;            
         
                 } 
                      }
                   else if(ch==1)
                   {   
                             if(ch==1)
                    {if(s1.menu_side_key==1 ||s1.menu_side_key==0)
                      {
                    s1.menu_side_key=5;
                      }
                      LCD_Clear();
                            LCD_DisplayString(*((--s1.menu_side_key)+menu_display));                            
          menu_position=1;   
                         }
                   }  
          }
      
    if(menu_position==0)
    goto repat;
        }while(ch!=5);
       
        a = s1.menu_side_key; 
  switch(a)
     { 
 case 1: // set time
          { 
    LCD_Clear();
       LCD_GoToLine(0);
       LCD_DisplayString(menu_display[5]);
    LCD_GoToLine(1);
    LCD_DisplayString("  HH:MM:SS:PM/AM");
    UP_Down_Keyvalue(s1,2,4);    
          break;
          } 
 case 2: // Set date
        {
         LCD_Clear();
         LCD_GoToLine(0);
         LCD_DisplayString(menu_display[6]);
      LCD_GoToLine(1);
      LCD_DisplayString("  DD:MM:YY");
      UP_Down_Keyvalue(s1,2,3);     
         break;
        }
 
 case 3: // set alarm
          {
         LCD_Clear();
            LCD_GoToLine(0);
         LCD_DisplayString(menu_display[7]);
      LCD_GoToLine(1);
      LCD_DisplayString(" HH:MM:SS:AM/PM");
      UP_Down_Keyvalue(s1,2,4);      
          break;
             }
 case 4:  // set alarm
           {
            LCD_Clear();
          LCD_GoToLine(0);
          LCD_DisplayString(menu_display[8]);
    LCD_GoToLine(1);
    LCD_DisplayString("  HH:MM:SS:PM/AM");
    UP_Down_Keyvalue(s1,2,4);    
          break;
            }
     } 
    
  while(Key_pressed()!=5); 
  
       
 } 
 
 
 
 
 /* Function Key Value For UP_Down Key */
 void UP_Down_Keyvalue(struct menu s1,int i,int j)
 {    
  
  int ch,lower,upper;    
  do{  
    if(j==4)
     {
      if(i==2)upper=1;
      if(i==3)upper=9;
      if(i==5)upper=5;
      if(i==8)upper =5;
      if(i==9)upper =9;
     } 
      if(UP_S)   
      {
      while(UP_S);    
      if(s1.menu_up_key==upper)
      s1.menu_up_key = lower-1;
      LCD_GoToXY(1,i); 
      LCD_Printf("%d",++s1.menu_up_key);
      s1.time[i-2]=s1.menu_up_key;          
      }
   else if(DOWN_S) // down 
             
          {    while(DOWN_S);
            if (s1.menu_up_key==lower)
            s1.menu_up_key = upper+1;        
            LCD_GoToXY(1,i);
            LCD_Printf("%d",--s1.menu_up_key);
      s1.time[i-2]=s1.menu_up_key;       
     }
          if(RIGHT_S)
       
             { while(RIGHT_S);
       s1.menu_up_key=0;
          if(i==9) goto exit1;
          if(i==3||i==6)
          ++i;
          i++;
              }
      exit1:
           if (LEFT_S)
             { while(LEFT_S);
        s1.menu_up_key=0;
            if(i==2) goto exit2;         
            if(i==5||i==8)
            --i;
            i--;
     
             }
    exit2:continue;
   
   
  } while (ch!=5);  // if Okay key exit loop
  
 }
 
 
 
See You 


Transformer-configuration-in-proteus

By

Setting up A Transformer in Proteus


The calculation Formula is  L1/L2 = (V1/V2)^2

For Example: Design for 6 Volt input ,230volt 50Hz Not considered wattage just for simulation. 

           Assign L1 = 1H (but 1henry expectable for a 10 or 20 kW transformer)

        Then L2 = 1/(230/6)^2
                       = 680.5293micro Henry



Transformer-configuration-in-proteus

By

Setting up A Transformer in Proteus


The calculation Formula is  L1/L2 = (V1/V2)^2

For Example: Design for 6 Volt input ,230volt 50Hz Not considered wattage just for simulation. 

           Assign L1 = 1H (but 1henry expectable for a 10 or 20 kW transformer)

        Then L2 = 1/(230/6)^2
                       = 680.5293micro Henry



gElectron. Powered by Blogger.