Eight MCU C language basic programming source codes

Publisher:清新风华Latest update time:2013-03-19 Source: 21IC Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. The P2 port of a certain single-chip computer system is connected to a digital-to-analog converter DAC0832 to output analog quantity. Now it is required to output a continuous triangular wave from the DAC0832. The implementation method is to continuously output the value that changes according to the triangular wave from the P2 port, starting from 0 and gradually increasing, and then gradually decreasing after reaching a certain maximum value until 0, and then gradually increasing from 0, and continue to output in this way. Try to write a function to make the value output from the P2 port generate a triangular wave, and make the period and maximum value of the triangular wave changeable through the input parameters.

#include              
#define DAC0832 XBYTE[0x7FFF]
void san(unsigned char max1,unsigned char zhou1)
{
    unsigned char i,j,max,zhou;
    max=max1;
    zhou=zhou1;
while(1)
    {
    for(i=0;i<=max,i++)
    {
        DAC0832=i;
        for(j=0;j            delay();
}
    for(i=max-1;i>0,i--)
    {
        DAC0832=i;
        for(j=0;j            delay();
}
}
 
2. In digital filtering, there is a technology called "median filtering", which is to sort the collected data from large to small or from small to large, and then take the number in the middle as the sampling value. Try to write a function to sort the sampled data of 20 cells starting from 0x50 in the on-chip data memory using the bubble method and perform median filtering, and store the obtained sampled data in cell 0x4f.
void midf()
{
    unsigned char data temp,i,j,*ptr,*ptr1;
    ptr=0x50;
ptr1=0x4f;
    for(j=20;j>1,j--)
{
    for(i=0;i    {
    if(*(ptr+i)>*(ptr+i+1))
    {
temp=*(ptr+i+1);
*(ptr+i+1)=*(ptr+i);
*(ptr+i)=temp;
            }
}
}
*ptr1=*(ptr+20/2);
}
 
3. In digital filtering, there is a technique called "extreme value average filtering", which is to sort the collected data from large to small or from small to large, then remove the same number of maximum and minimum values, and calculate the arithmetic mean of the middle part of the data as the sampling value. Try to write a function to perform "extreme value average filtering" on the sampling data of 16 units starting from 0x150 stored in the off-chip data memory using the selection method, and store the obtained sampling data in the 0x14f unit.
void teaf()
{
    unsigned char xdata *ptr;
unsigned char data temp,i,j,*ptr1;
    ptr=0x150;
ptr1=0x4f;
    for(j=16;j>1,j--)
{
    for(i=0;i    {
    if(*(ptr+i)>*(ptr+i+1))
    {
temp=*(ptr+i+1);
*(ptr+i +1)=*(ptr+i);
*(ptr+i)=temp;
            }
}
}
temp=0;
for(i=0;i<16-2;i++)
temp=temp+*(ptr+i+1);
    *ptr1=temp/(16-2);
}[page]
 
4. Use the library function _getkey to write a function to implement a C51 program that receives data from the serial port of the microcontroller. The received data is stored in the area starting from 0x40 in the on-chip data memory and ends when a carriage return character CR (ASCII code is 0x0d) is encountered.
include
void main()
{
    unsigned char data *dpt;
    unsigned char i=0,temp;
    dpt=0x40;
while(1)
{
    temp=_getchar();
    if(temp==0x0d)
break;
    *(dpt+i)=temp;
    i++;
}
}
 
5. Use the library function scanf to write a function to implement a C51 program that receives data from the serial port of the microcontroller. The received data is stored in the area starting from 0x240 in the off-chip data memory and ends when a carriage return character CR (ASCII code is 0x0d) is encountered.
include
void main()
{
    unsigned char xdata *xdpt;
    unsigned char i=0,temp;
    xdpt=0x240;
while(1)
{
    scanf(“%c”,&temp);
    if(temp==0x0d)
break;
    *(xdpt+i)=temp;
    i++;
}
}
 
6. Use the library function putchar to write a function to implement a C51 program that sends data from the serial port of the single-chip microcomputer. The data to be sent is stored in the area starting from 0x50 in the on-chip data memory and ends when a carriage return character CR (ASCII code is 0x0d) is encountered.
include
void main()
{
    unsigned char data *dpt;
    unsigned char i=0;
    dpt=0x50;
    while(*(dpt+i)!=0x0d)
    {
    putchar(*(dpt+i));
    i++;
}
}
 
7. Use the library function printf to write a function to implement a C51 program that sends data from the serial port of the single-chip microcomputer. The data to be sent is stored in the area starting from 0x100 in the off-chip data memory and ends when a carriage return character CR (ASCII code is 0x0d) is encountered.
include
void main()
{
    unsigned char xdata *xdpt;
    unsigned char i=0;
    xdpt=0x100;
    while(*(xdpt+i)!=0x0d)
    {
    printf(“%c”,*(xdpt+i));
    i++;
}
}
 
8. Write a timer/counter 0 timing 100ms interrupt processing function to make the function realize the clock function, that is, generate the seconds, minutes and hours of the clock. The high and low bytes of the initial value are set to TIMER_H and TIMER_L respectively.
Assume fosc=6mhz
#include
sfr TIMER_H=0x8b;
sfr TIMER_L=0x8a;
unsigned char data shi,fen,miao,bfm;
 
void main()
{
    TMOD=0x01;
    TIMER_H=15536/16;
    TIMER_L=15536%16;
    ET0=1;
    EA=1;
    TR0=1;
    while( 1);
}
 
void time0_int() interrupt 1
{
    TIMER_H=15536/16;
    TIMER_L=15536%16;
    bfm++;
    if(bfm==10)
    {
    bfm=0;
    miao++;
}
    if(miao==60)
    {
    miao=0;
    fen++;
}
    if(fen==60)
    {
    fen=0;
    shi++;
}
    if(shi==24)
    {
    shi=0;
}
}

Keywords:MCU Reference address:Eight MCU C language basic programming source codes

Previous article:Microcontroller delay program experience
Next article:The structure and working principle of single chip microcomputer timer/counter

Recommended ReadingLatest update time:2024-11-16 16:53

If you want to be comfortable with microcontrollers, understand these triode knowledge!
In fact, as long as you understand the characteristics of the triode, it will be much easier for you to use the microcontroller. Everyone actually knows that the triode has an amplifying effect, but how to truly understand it is the key to whether you will use most electronic circuits and 1C in the future. What we ge
[Microcontroller]
51 MCU Experiment 3: LED Flowing Light
The circuit diagram of the LED module of the development board is as follows: The header file for the circular left/right shift function is "intrins.h" #include reg52.h #include intrins.h #define uc unsigned char void delay(uc n) {     uc i,j;     for(i=1;i =n;i++)       for(j=110;j =1;j--); } void main() {    
[Microcontroller]
51 MCU Experiment 3: LED Flowing Light
Serial communication - data is sent to the PC and then added by 1 and displayed on the digital tube after being received by the microcontroller
Download the complete schematic diagram of this program:  http://www.51hei.com/f/ks51.pdf   /** *Function: Serial communication - data is sent to PC, incremented by one, and then received by MCU and displayed on the digital tube *Time: March 2, 2014 10:04:07 *Author: Han Zhuzi *Remarks: Baud rate: 19200bps *Summ
[Microcontroller]
51 single chip microcomputer read and write max186 c51 program
//ad conversion subroutine //The returned data is a 12-bit int //max186 channel ch0 corresponds to 0, which is max186 pin 1 //max186 channel ch1 corresponds to 1, which is max186 pin 2 //max186 channel ch2 corresponds to 2, which is max186 pin 3 //max186 channel ch3 corresponds to 3, which is max186 pin 4 //max1
[Microcontroller]
51 single chip microcomputer read and write max186 c51 program
MSP430G2553 MCU uses printf function to print out serial port
When I used Keil to write 51 MCU programs, I often used the printf function to print out some key process data to the computer to monitor the running status of the program. Recently, when I used IAR for MSP430 to debug the MSP430G2553 program, I found some small problems. The MSP430G2553 MCU did not output data to the
[Microcontroller]
MSP430G2553 MCU uses printf function to print out serial port
Design of automotive ASIL-D compliant watchdog application
At present, while the development momentum of automobile automated driving is rising steadily, various painful quality accidents frequently occur. Demand promotes change, and demand promotes the rapid implementation of functional safety in automotive electronics. This article will take you to appreciate the watchdog i
[Embedded]
Design of automotive ASIL-D compliant watchdog application
How to write the header file of 51 single-chip microcomputer?
I have searched for him everywhere, but I still cannot understand your code! I believe that for friends who are new to microcontroller programming or other language programming, when we first start programming, we always like to write all the codes in one file. (Of course, if you are a genius, that's another story
[Microcontroller]
51 MCU identifies whether four independent buttons are short pressed or long pressed
This is a reference program that can identify whether four independent buttons are short pressed or long pressed. This program has been compiled and passed. Beginners can transplant it to the 51 microcontroller to try it out. /************************************************/ //FileName: Identify whether four indepe
[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号