Microcontroller C language program example (Part 3)

Publisher:SereneWhisperLatest update time:2016-02-25 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
14. Music Playback

//This program has been debugged on the hardware.
//The MCU crystal oscillator of this program adopts 11.0592M
#include
sbit      speaker=P1^2; 
unsigned char timer0h,timer0l,time;
       //There is only one good mother in the world data table
code unsigned char sszymmh[]={        6,2,3,      5,2,1,      3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1, 6,2,4,
                                     3,2,2          5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1, 6,1,1, 5,2,1,
                                     3,2,1          2,2,4, 2,2,3, 3,2,1, 5,2,2,
                                     5,2,1,      0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8  , 0xF9,0xF9,0xFA, 0xFA, 0xFB,0xFB,0xFC,0xFC, // 1,2,3,4,5,6,7,8      , i  0xFC ,0xFD,0xFD,0xFD,0xFD , 0xFE, 0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF, } ; // The lower eight bits of the scale frequency table code unsigned char FREQL[]={ 0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6, 0x21,0xE1,0x8C,0xD8,0x68,0xE9,0 x5B,0x8F, //1,2,3,4,5,6,7,8,i 0xEE,0x44, 0x6B,0xB4,0xF4,0x2D, ​​0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16, }; void delay(unsigned char t) { unsigned char t1; unsigned long t2; for(t1=0;t1                                              
                                     
      

                               
                               
                               
                               
                              
        

                                
                                
                                
                                
                               

  
  
  
  
  
   
      
       
       
  
  
 


 
 
 
 
 
 


 
 
 
                      
 

 void main(void)
 {
 unsigned char k,i;
 TMOD=1; //Set CT0 timing working mode 1
 EA=1;
 ET0=1;//IE=0x82 //CPU opens interrupt, CT0 opens interrupt
 while(1)
     {
      i=0; 
      while(i<100){         //Music array length, start over after singing        
      k=sszymmh[i]+7*sszymmh[i+1]-1;
      timer0h=FREQH[k];
      timer0l=FREQL[k];
      time=sszymmh[i+2];
      i=i+3;
      song();
       }
     }
 }

15. Infrared transceiver demonstration


#include


sbit LED=P3^3; //Infrared emission
sbit LED1=P1^3;
sbit IR=P3^2; //Infrared integrated reception

 bit Flag;

void Init_Timer0(void)
{
 TMOD |= 0x01;       
 TH0=0xf0;                
 TL0=0x00;
 EA=1;                     
 ET0=1;                    
 TR0=1; 
}

 

void Timer0_isr(void) interrupt 1 using 1
{
 TH0=0x0f;  
 TL0=0x00;
 Flag=!Flag;
}


main()
{

 unsigned int j;
 Init_Timer0();


 while(1)
 {

   LED1=IR; //Read the value of the integrated receiving head
 if(Flag)
   {
    for(j=0;j<27;j++) //About 38KHz
    {
     LED=!LED; //Transmitter output
     }
    }
  }
}

16.Serial communication

//Open the serial port debugging program and set the baud rate to 2400 with no parity check


#include                
                                 

#include                


#ifdef MONITOR51                        
char code reserve [3] _at_ 0x23;        
#endif                                  
                                        
sbit KEY=P3^2;

void delay(unsigned int U)
{
 while(--U);
}

void main (void)
{


#ifndef MONITOR51
    SCON  = 0x50;         
    TMOD |= 0x20;              
    TH1   = 0xf3;               
    TR1   = 1;                 
    TI    = 1;                 
#endif


  while (1)
    {
   
    if(!KEY)
       {
        delay(10000);
        if(!KEY)
          {
          printf ("This programer test ok!n"); 
         printf ("12:25:26n"); 
           }
  }
    }
}

17.PWM dimming

 

#include

sbit LED = P1^2;
unsigned char CYCLE; //Define the cycle. If the digital X reference timing time is 10, the cycle is 10 x 0.1ms
unsigned char PWM_ON ;//Define the high level time
void delay(unsigned int cnt)
{
 while(--cnt);
}

main()
{
bit Flag;

TMOD |=0x01; //Timer setting 0.1ms in 12M crystal
TH0=(65536-100)/256;
TL0=(65536-100)%256; //Timing 0.1mS
IE= 0x82;  //Open interrupt
TR0=1;

CYCLE = 10; // The time can be adjusted. This is 10. Adjusting 8-bit PWM means 256 steps.
while(!Flag)
 {
  delay(20000); // Delay time, the interval from one brightness to the next brightness. A fast speed can show the continuous effect.
  PWM_ON++;     // This uses a longer delay so that the change process can be seen clearly.
  if(PWM_ON == CYCLE)
             // Other programs can be added here, such as controlling the device at the brightest time.
     Flag=1;
  }
 }

 while(Flag)     //The brightness decreases as above, which is a reverse process
 {
  delay(20000);
  PWM_ON--;
  if(PWM_ON == 0)
    {
     Flag=0;
  }
 }
}



void tim(void) interrupt 1 using 1
{
static unsigned char count; //
TH0=(65536-100)/256;
TL0=(65536-100)%256;//Timing 0.1mS

if (count==PWM_ON)
   {
    LED = 1; //light off
   }
 count++;
if(count == CYCLE)
   {
   count=0;
 if(PWM_ON!=0) //If the left and right time is 0, keep the original state
   LED = 0; //light on

    }

}


Keywords:MCU Reference address:Microcontroller C language program example (Part 3)

Previous article:Program to drive ST7565P LCD screen based on SPI of ATMEGA8515
Next article:Microcontroller C language program example (I)

Recommended ReadingLatest update time:2024-11-16 19:36

51 MCU Series Knowledge 7--Interrupt System (2)
2. Supplement and expansion 1. Generation of interrupt request signal ①1NT0 and 1NT1: external interrupt 0 and external interrupt 1. The interrupt request signals are input by P3.2 and P3.3 pins respectively. The valid level of the request signal is set by IT0 and IT1. Once the input signal is valid, the IE0 or IE
[Microcontroller]
51 MCU Series Knowledge 7--Interrupt System (2)
Detailed explanation of DS18B20 temperature sensor driver based on 51 single chip microcomputer
//This part is the driver of 18B20   //This program has been verified, the crystal oscillator is 12MHz   #include reg52.H #include intrins.h   sbit D18B20=P3^7; //DQ connects to P3^7 #define NOP() _nop_()   #define _Nop() _nop_()    void TempDelay (unsigned char idata us); //delay function defini
[Microcontroller]
How to effectively implement infrared communication function in single chip microcomputer
  The multi-rate watt-hour meter is an indispensable switch power supply module power metering product under the current power saving and planned power consumption policies in my country. The communication interface of the multi-rate watt-hour meter generally has both infrared interface and RS485 interface. Infrared c
[Microcontroller]
Design and simulation of high-power solar LED street lamp based on single chip microcomputer
Introduction: This paper mainly introduces the design of solar LED street light panels, the selection of solar cells and batteries, and analyzes and discusses the circuit design and working principle of each part of the street light in detail. Protues and Keil software are used to simulate the charging circuit, boost
[Power Management]
Design and simulation of high-power solar LED street lamp based on single chip microcomputer
51 MCU C language program (V) Clock program (using timer)
#include #define uchar unsigned char #define uint unsigned int sbit dula=P2^6; sbit wela=P2^7; sbit rs=P3^5; sbit lcden=P3^4; sbit s1=P3^0; sbit s2=P3^1; sbit s3=P3^2; sbit rd=P3^7; uchar count,s1num; char miao,shi,fen; uchar code table ="  2009-7-13 MON"; uchar code table1 ="      00:00:00"; void delay
[Microcontroller]
Application of MCU in mobile phones and tablet computers
  MCU is mainly used as a co-processor for many functions such as capacitive touch sensing interface, touch screen interface, camera interface, different analog sensor input detection, USB interface, battery charging and monitoring, etc. In addition, all the logic and interfaces responsible for interconnecting the abo
[Power Management]
Application of MCU in mobile phones and tablet computers
Design of water flow control device based on single chip microcomputer
  0 Introduction   With the development of the times and the advancement of science and technology, flow-related sensor technology has become more and more mature and diverse. According to the structural type of flow sensors, they can be divided into blade (wing plate) type, core type, hot wire type, hot film type,
[Microcontroller]
Design of water flow control device based on single chip microcomputer
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号