-->

16x2 LCD Interfacing with STM32,STM32F103C6

By

 16x2 LCD  Interfacing with STM32,STM32F103C6






  

lcd_init();

   LCD_LINE1;
  lcd_String("  GeElectron");
  LCD_LINE2;
   lcd_String("   Welcome");

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

	  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, GPIO_PIN_SET);// LCD Enable PIN to High
	 	  	  HAL_Delay(500);
	 	  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, RESET);
	  	      HAL_Delay(500);
	  	  //  lcd_String("Hello");

  }
  /* USER CODE END 3 */
}
LCD.c File


/*
 * lcd.c
 *
 *  Created on: Sep 30, 2023
 *      Author: Geelabs
 */
#include "stm32f1xx_hal.h"
#include "lcd.h"
#include "main.h"



extern void Delay_Us (uint16_t us);
void lcd_Strobe(void)
{

	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11, GPIO_PIN_SET);// LCD Enable PIN to High
    HAL_Delay(1);   
    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11, RESET);// LCD Enable High to Low
    HAL_Delay(1);
 }

void lcd_init(void)
{

  HAL_Delay(40);
  lcd_cmd(0x03);
  HAL_Delay(5);
  lcd_cmd(0x03);
  HAL_Delay(1);
  lcd_cmd(0x03);
  HAL_Delay(10);
  lcd_cmd(0x02); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
  HAL_Delay(1);
  lcd_cmd(0x28);
  lcd_cmd(0x08);
  HAL_Delay(10);
  lcd_cmd(0x00);
  lcd_cmd(0x01);
  HAL_Delay(3);
  lcd_cmd(0x00);

   lcd_cmd(0x0c);            // Make cursorinvisible
   lcd_Clear();            // Clear screen
   lcd_cmd(0x6);            // Set entry Mode(auto increment of cursor)
}

void lcd_cmd(__uint16_t cmd)
{
	    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10, GPIO_PIN_RESET);
        cmd = (cmd << 8) ;
        GPIOB->ODR =  (cmd & 0xf000);
        lcd_Strobe();
        cmd = (cmd << 4) ;
        GPIOB->ODR =  (cmd & 0xf000);
        lcd_Strobe();
}


void lcd_data(__uint16_t lcd_data)
{
    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10, GPIO_PIN_SET);
     lcd_data = (lcd_data << 8) ;
     GPIOB->ODR =  (lcd_data& 0xf000)|0x400;
     lcd_Strobe();
     lcd_data = (lcd_data << 4) ;
     GPIOB->ODR =  (lcd_data & 0xf000)|0x400;
     lcd_Strobe();
   }


void lcd_Clear(void)
{
    lcd_cmd(0x01);
    HAL_Delay(5);
}

void lcd_String(const char *ptr)  // ptr pointing the address of displaying string
{
    while (*ptr)             // check weather the endo of file then exit the loop
    {
        lcd_data(*ptr++);    // Sending data to LCD_data functions
    }
}




By

The 16F877 is a microcontroller developed by Microchip Technology, which is capable of supporting the Serial Peripheral Interface (SPI) communication protocol. The SPI protocol is a synchronous serial communication protocol that allows devices to exchange data with each other. In order to use the SPI protocol with the 16F877 microcontroller, you need to follow these steps:


Configure the SPI pins: The 16F877 has dedicated pins for SPI communication, which are RB0 (SS), RB1 (SDO), RB2 (SDI), and RB3 (SCK). You need to configure these pins as digital I/O pins and set their direction as input or output, depending on the functionality you want to implement.


Configure the SPI module: The 16F877 has an SPI module that can be configured using the SPBRG (SPI Baud Rate Generator) register. This register sets the clock speed for the SPI communication, and its value depends on the system clock frequency and the desired baud rate. You also need to configure the SPI control register (SSPSTAT) and the SPI control register (SSPCON), which define the SPI mode, clock polarity, and data order.


Implement SPI communication: Once the SPI pins and module are configured, you can implement SPI communication between the 16F877 microcontroller and other SPI devices. To send data from the 16F877 to another device, you need to write the data to the SPI buffer register (SSPBUF) and then wait for the transfer to complete. To receive data from another device, you need to read the data from the SPI buffer register after the transfer has completed.


Here is some sample code in C language for implementing SPI communication using the 16F877 microcontroller: 




 #include <pic16f877.h>  
 #include <xc.h>  
 void spi_init()  
 {  
   // Configure SPI module  
   SSPCON = 0b00100000; // SPI Master, clock = Fosc/4  
   SSPSTAT = 0b00000000; // Data sampled at middle of data output time  
   // Configure pins  
   TRISC5 = 0; // SDO as output  
   TRISC4 = 1; // SDI as input  
   TRISC3 = 0; // SCK as output  
 }  
 void spi_write(char data)  
 {  
   SSPBUF = data; // Write data to buffer  
   while (!SSPIF); // Wait for transmission to complete  
   SSPIF = 0; // Clear flag  
 }  
 char spi_read()  
 {  
   SSPBUF = 0x00; // Write dummy data to initiate read  
   while (!SSPIF); // Wait for reception to complete  
   SSPIF = 0; // Clear flag  
   return SSPBUF; // Return received data  
 }  
 void main()  
 {  
   spi_init();  
   spi_write(0x55); // Write data to SPI  
   char data = spi_read(); // Read data from SPI  
 }  
 Note that this is just a simple example, and the specific configuration of the SPI module will depend on the requirements of your particular project.  

OLED Interface with PIC16f877A by using SPI Communication

By
To interface an OLED display with SPI using a PIC16F877A microcontroller, you can follow these general steps: Choose an OLED display that supports SPI communication. Make sure to read the datasheet and understand how to communicate with the display using SPI. Configure the SPI module of the PIC16F877A. The PIC16F877A has a built-in SPI module that can be configured to work in Master mode. You will need to set the appropriate registers to configure the SPI module, such as the SSPCON register. Write a software program to communicate with the OLED display. This program should send commands and data to the display using the SPI module of the PIC16F877A. The program will need to follow the communication protocol specified in the OLED display datasheet. Connect the OLED display to the SPI pins of the PIC16F877A. You will need to connect the MOSI (Master Out Slave In), MISO (Master In Slave Out), and SCK (Serial Clock) pins of the PIC16F877A to the corresponding pins on the OLED display. You may also need to connect additional pins for chip select (CS) and data/command select (DC). Test the OLED display. Once you have written the program and connected the OLED display, you can test the communication by sending commands and data to the display and verifying that it responds as expected.
       

            #include 

// Configuration bits
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF

// Function to initialize the SPI module
void spi_init()
{
    TRISC5 = 0; // SDO pin as output
    TRISC4 = 1; // SDI pin as input
    TRISC3 = 0; // SCK pin as output
    TRISA5 = 0; // CS pin as output
    
    SSPSTAT = 0x00;
    SSPCON = 0x20;
}

// Function to send a command to the OLED display
void oled_cmd(unsigned char cmd)
{
    RA5 = 0; // Set CS low to select the OLED display
    
    SSPBUF = 0x00; // Send the command byte
    while(!SSPSTATbits.BF); // Wait for the transmission to complete
    
    RA5 = 1; // Set CS high to deselect the OLED display
}

// Function to send data to the OLED display
void oled_data(unsigned char data)
{
    RA5 = 0; // Set CS low to select the OLED display
    
    SSPBUF = 0x40; // Send the data byte
    while(!SSPSTATbits.BF); // Wait for the transmission to complete
    
    RA5 = 1; // Set CS high to deselect the OLED display
}

// Main function
void main()
{
    spi_init(); // Initialize the SPI module
    
    oled_cmd(0xAE); // Display off
    
    // Send initialization commands (see SSD1306 datasheet for details)
    oled_cmd(0xD5);
    oled_cmd(0x80);
    oled_cmd(0xA8);
    oled_cmd(0x3F);
    oled_cmd(0xD3);
    oled_cmd(0x00);
    oled_cmd(0x40);
    oled_cmd(0x8D);
    oled
}

       
 

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


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



Atmeag8 /AVR based Voltmeter

By
Tutorial for Atmeag8 /AVR based Voltmeter

               It measures the Ac voltage and shows readings in RMS value.

Basics Of AC signal 

The AC signal ,its amplitude varying in accordance with time. In india AC frequency is 50Hz.

Then the Time period one full cycle ,
        
  T = 1/frequency 
  T = 1/50Hz = .002second 
                     = 20milli Second
                     = 10 Milli second for Half Cycle








There are a lot of methods for measuring AC volt digitally

we could find the RMS value by multipliying the Peak Voltage with 0.707. 

   Vrms = Peak Voltage * 0.707 .

Approximatly Indian Line AC Peak volatge is 325v.

  Example : 
                 325v *0.707 = 229.775Volt      .(appro = 230Vrms).

But Microcontroller could only sense  up to 5 volt  as well as it couldnt sense negative values

so we cannot  give 230 volt directly to It .
It can be rectify by using a Stepdown transformer ,which has an advantage of safety.
Ac voltage can be reduce by using transformer to comfortable range . (ie 0 to 5volt DC)

make the calculation  as   ,
 The voltage Divider would give a 5 volt when the Vrms 230v. [will be more give details on the way ].


The full wave rectifier output will give to ADC of micro controller . Then do corresponding coding.

Microcontroller Section 
I am using here Amega8. 
we need to mesure only one half cycles repeatedly because  the waves same are repeating 
Basics

I selected , the F_cpu frequency is the internal RC Oscillator 8MHz.
Atmega8 ADC uses Prescalar [frequency reducer or divider by 2th,4th etc]
Atmega ADC  Prescale Frequency is in between 50Khz to 200Kz
ADC conversion time is in between 13 to 260 micro second.

ADC frequency = F_cpu / Prescaler

                         = 800,000HZ  / 8
  
                        = 62.5KHz

At high frequency ADC conversion would be fast while at lower frequency the conversion will be more accuracy.

ADC conversion time Calculation 

Conversion Time = 1/ADC frequency

                           = 1/62500 
                           = 0.000016 Second
                           = 0.016 Milli Second
                           = 16 Micro Second.     [At 62.500KHz]

So 16 mirco second would taken for one cycle of  conversion.with 62,5KHz.

13 ADC cycles required for one conversion

ie,
    13 * 16Micro Second = 230 Micro Second [approximately]
                                      = 230us.

The Half wave has 10milli second time duration.[100000microsecond].

There the available conversion times is 

                                        =  Total time period for a half cycle /one ADC conversion time  
                                        
                                        = 10000 micro second / 230 micro second
                 
                                        = 43.4 samples  [ for one half cycleie 10 milli second]

                                       
    Image Shows only 7 Samples per half cycles .in our case it is 43

And again reduced the sampling to 40 .

calculation of Vpeak

 Takes the largest Vpeak from the 40 samples.
Vrms = Vpeak * .707
*****************************************************************************

Atmega8 ADC StepSize Calculation

The Atmega8 ADC is a 10 Bit /[ie the Maximum high value is 1023 =1111111111] at which the ADC will give fullscale Value .
The ADC  require a clock as well as a reference voltage .
The resolution of ADC output decide the Step size.

ADC o/p = Vin * 1024 / Vref

Examples;
  Vin =5volt , Vref =5volt 

                 ADC o/p = 5 * 1023 /5 = 1023 [ADC highest Value]

               Step Size  voltage = Vref / 1023 = 0.004887 volt =4.88millivolt
             One binary digit changes when 4.88volt changes takes place


           

     

Image's Step Size is 1.25 volt




*******************************************************************************************************

Atmeag8 /AVR based Voltmeter

By
Tutorial for Atmeag8 /AVR based Voltmeter

               It measures the Ac voltage and shows readings in RMS value.

Basics Of AC signal 

The AC signal ,its amplitude varying in accordance with time. In india AC frequency is 50Hz.

Then the Time period one full cycle ,
        
  T = 1/frequency 
  T = 1/50Hz = .002second 
                     = 20milli Second
                     = 10 Milli second for Half Cycle








There are a lot of methods for measuring AC volt digitally

we could find the RMS value by multipliying the Peak Voltage with 0.707. 

   Vrms = Peak Voltage * 0.707 .

Approximatly Indian Line AC Peak volatge is 325v.

  Example : 
                 325v *0.707 = 229.775Volt      .(appro = 230Vrms).

But Microcontroller could only sense  up to 5 volt  as well as it couldnt sense negative values

so we cannot  give 230 volt directly to It .
It can be rectify by using a Stepdown transformer ,which has an advantage of safety.
Ac voltage can be reduce by using transformer to comfortable range . (ie 0 to 5volt DC)

make the calculation  as   ,
 The voltage Divider would give a 5 volt when the Vrms 230v. [will be more give details on the way ].


The full wave rectifier output will give to ADC of micro controller . Then do corresponding coding.

Microcontroller Section 
I am using here Amega8. 
we need to mesure only one half cycles repeatedly because  the waves same are repeating 
Basics

I selected , the F_cpu frequency is the internal RC Oscillator 8MHz.
Atmega8 ADC uses Prescalar [frequency reducer or divider by 2th,4th etc]
Atmega ADC  Prescale Frequency is in between 50Khz to 200Kz
ADC conversion time is in between 13 to 260 micro second.

ADC frequency = F_cpu / Prescaler

                         = 800,000HZ  / 8
  
                        = 62.5KHz

At high frequency ADC conversion would be fast while at lower frequency the conversion will be more accuracy.

ADC conversion time Calculation 

Conversion Time = 1/ADC frequency

                           = 1/62500 
                           = 0.000016 Second
                           = 0.016 Milli Second
                           = 16 Micro Second.     [At 62.500KHz]

So 16 mirco second would taken for one cycle of  conversion.with 62,5KHz.

13 ADC cycles required for one conversion

ie,
    13 * 16Micro Second = 230 Micro Second [approximately]
                                      = 230us.

The Half wave has 10milli second time duration.[100000microsecond].

There the available conversion times is 

                                        =  Total time period for a half cycle /one ADC conversion time  
                                        
                                        = 10000 micro second / 230 micro second
                 
                                        = 43.4 samples  [ for one half cycleie 10 milli second]

                                       
    Image Shows only 7 Samples per half cycles .in our case it is 43

And again reduced the sampling to 40 .

calculation of Vpeak

 Takes the largest Vpeak from the 40 samples.
Vrms = Vpeak * .707
*****************************************************************************

Atmega8 ADC StepSize Calculation

The Atmega8 ADC is a 10 Bit /[ie the Maximum high value is 1023 =1111111111] at which the ADC will give fullscale Value .
The ADC  require a clock as well as a reference voltage .
The resolution of ADC output decide the Step size.

ADC o/p = Vin * 1024 / Vref

Examples;
  Vin =5volt , Vref =5volt 

                 ADC o/p = 5 * 1023 /5 = 1023 [ADC highest Value]

               Step Size  voltage = Vref / 1023 = 0.004887 volt =4.88millivolt
             One binary digit changes when 4.88volt changes takes place


           

     

Image's Step Size is 1.25 volt




*******************************************************************************************************

Microcontroller based Digital AC Meter Part 2 Code And Circuit

By
 Microcontroller based Digital AC Meter Part 2 Code And Circuit

First Part available here



Circuit Diagram 

#include"lcd.h"
#include "adc.h"
#include "delay.h"
double volt_read_disply();
int main(void)

{
int adc_value1=0;
LCD_SetUp(PB_0,PB_1,PB_2,P_NC,P_NC,P_NC,P_NC,PB_4,PB_5,PB_6,PB_7);
LCD_Init(2,16);
adc_init();

LCD_Clear();
LCD_GoToXY(0,0);

LCD_DisplayString(" g-Electron");
_delay_ms(1000);

while(1)
{
adc_value1 = volt_read_disply();
LCD_GoToLine(1);
LCD_DisplayString(" Volt : ");
LCD_DisplayNumber(10,adc_value1,3);
LCD_DisplayString("V");
_delay_ms(1300);
}
}

double volt_read_disply()
{ int i,adc_value[40]={0}; int temp =0;

adc_init();

for(i=0; i<40;i++) // samples taking from 41 times 9.4milli second taken about on complte half cycle
{
adc_value[i] = read_adc(0); // reading voltage
}

temp = adc_value[0];
for(i=0; i<40; i++)
{
if(temp<adc_value[i])
temp=adc_value[i];
}

return ((double)temp*0.224828935); // int to double casting

}

More Posts

gElectron. Powered by Blogger.

Contributors

16x2 LCD Interfacing with STM32,STM32F103C6

 16x2 LCD  Interfacing with STM32,STM32F103C6 lcd_init(); LCD_LINE1; lcd_String(" GeElectron"); LCD_LINE2; lc...

Contact Form

Name

Email *

Message *

Contact us

Name

Email *

Message *

Follow Us

https://www.facebook.com/gElectron-393939667321867/ FBbox/https://www.facebook.com/IVYthemes

Comments

[blogger]

MKRdezign

Test

Latest

[recent][newsticker]

Technology

Top Ads

RECENT COMMENTS

Subscribe Via Email

Subscribe to our newsletter to get the latest updates to your inbox. ;-)


Your email address is safe with us!

RECENT COMMENTS