C language programming of 24C08 data operation based on AVR microcontroller

Publisher:SereneSoul55Latest update time:2016-10-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#include
#define uint unsigned int
#define uchar unsigned char
#include "I2C_drive.h" //Includes the I2C bus driver software package
uchar const seg_data[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff}; //Segment table for 0~9 and the extinguisher
uchar const bit_tab[]={0xbf,0x7f}; //Bit selection table for the 7th and 8th digital tubes
uchar disp_buf[2]={0,0}; //Define 2 display buffer units
uchar val;
#define beep_0 (PORTD=PORTD&0xbf) //The buzzer on PD7 sounds
#define beep_1 (PORTD=PORTD|0x40) //The buzzer on PD7 does not sound
/********Function: Delay function********/
void Delay_ms(uint xms)
{
  int i,j;
  for(i=0;i   { for(j=0;j<1140;j++) ; }
}
/*********The following is a function for the buzzer to sound********/
void beep()
{
uint bb;
for (bb=0;bb<20;bb++)
{
  beep_0; //The buzzer sounds
  Delay_ms(1); 
  beep_1; //Turn off the buzzer
  Delay_ms(1);  
  }  
}
/********Port setting function********/
void port_init(void)
{
 PORTA = 0xFF; //Output high level
 DDRA = 0xFF; //Set to output
 PORTC = 0xff; //Output high level
 DDRC = 0xFF; //Set to output
 DDRD =(0<  PORTD = 0xFF; //PD6 outputs high level, others are inputs with pull-up
}
/********Timer 0 initialization********/
void timer0_init()
{
 SREG = 0x80; //Enable global interrupt
 TIMSK|=(1<  TCCR0|=(1<  TCNT0 = 240; //Timing initial value setting, timing time 2ms
}
/********The following is the display function********/
void Display()
{
 uchar tmp; //Define display temporary storage
 static uchar disp_sel=0; //Display bit select counter, the display program uses it to know which digital tube is currently displayed, the initial value is 0
 tmp=bit_tab[disp_sel]; //Determine which digital tube to display based on the current bit select count value
 PORTC=tmp; //Send to PC to control the selected digital tube to light up
 tmp=disp_buf[disp_sel]; //Look up the display code of the number based on the current bit select count value
 tmp=seg_data[tmp]; //Get the display code
 PORTA=tmp; //Send to PA port to display the corresponding number
 disp_sel++; //Add 1 to the bit select count value and point to the next digital tube
 if (disp_sel==2)   
 disp_sel=0; //If 2 digital tubes are displayed once, return them to 0 and scan again

}
/********The following is the timer T0 interrupt function, used for dynamic scanning of the digital tube********/
#pragma interrupt_handler timer0_ovf:10
void timer0_ovf(void)
{
  TIFR=0x01; //Write 1 to clear the timer T0 flag
 TCNT0=240; //Set the initial count value and set the timing time to 2ms
 Display(); //Call the display function
}

/********The following is the main function********/
void main()
{
 timer0_init();
 I2C_init();
 port_init();
 Delay_ms(100);
 val=read_EEPROM(0x02);
 
 if(val>=100) val=0; //Prevent errors when reading EEPROM data for the first time
 while(1)
 { 
  
  if((PIND&0x04)==0) //If K1 key is pressed
  {
   Delay_ms(10); //Delay 10ms to debounce
   if((PIND&0x04)==0)
 
   {
    while(!(PIND&0x04)); //Wait for K1 key to be released
    val++;
    write_EEPROM(val,0x02);
    beep();
    if(val==99)val=0;
   }
  }
  disp_buf[0]=val/10;
   disp_buf[1]=val%10; 
 }
}

Reference address:C language programming of 24C08 data operation based on AVR microcontroller

Previous article:C language programming of mega16 internal EEPROM operation based on AVR microcontroller
Next article:C language programming of DS1302 digital tube time display based on AVR microcontroller

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号