// Main.c
//----------------------------------------------------------------------------------------------------
// Copyright (C) 2013 Shenyang JYXD.
// WEN BO RUI
// Tool chain: KEIL Full 'c'
//
//#pragma CD OE DB SB // Compilation directives
#include
#include
#define uchar unsigned char
#define uint unsigned int
uint Period; //Period refresh perioduint
PWM;
uint Pulse; //Number of pulsesuchar
Speed; //Speed variableuchar
count;
uint excount;
bit Gra; //Gradual change/jump control
bitbit Gra7; //Colorful gradual light and extinguishing control bitbit
Gra3; //Red, green and blue gradual light and extinguishing control bitbit
BP; //Light to dark, dark to light conversion bitbit
LEDdelay; //LED timing to flag
bitsbit key1 = P3^1; //Pattern combination key 1
sbit key2 = P3^0; //pattern key combination 2
sbit key3 = P3^5; //speed key combination 1
sbit key4 = P3^4; //speed key combination 2
uchar led1[12]={0x10,0x030,0x20,0x60,0x40,0x50,0x70,0x50,0x40,0x60,0x20,0x30};//red, yellow, green, cyan, blue, pink, white, pink, blue, cyan, green, yellow
uchar led2[24]={0x10,0x00,0x30,0x00,0x20,0x00,0x60,0x00,0x40,0x00,0x50,0x00,
0x70,0x00,0x50,0x00,0x40,0x00,0x60,0x00,0x20,0x00,0x30,0x00};
uchar led3[3]={0x10,0x20,0x40}; //Red, green, blue
uchar ledcount,ledcount1;
//***********************************************************
//The following functions are defined
// 1 millisecond delay function delayms(); pattern and speed scanning function keyscan();
//***************************************************************
/*void delayms(unsigned int ms)
{
unsigned char i ;
while(ms--)
{
for(i = 0 ; i < 120 ; i++) ;
}
}*/
void Pattern1(); //Pattern 1 function--------->No light off process when changing
void Pattern2(); //Pattern 2 function--------->The transition has a light-off processvoid
Pattern34();
void keyscan()
{
if((key4==1)&&(key3==1)) {Speed = 20;Period = 25;} //Preset 4 speeds 200MS
if((key4==0)&&(key3==1)) {Speed = 50;Period = 50;} //500MS
if((key4==1)&&(key3==0)) {Speed = 100;Period = 100;} //1S
if((key4==0)&&(key3==0)) {Speed = 150;Period = 200;} //1.5S
if((key2==1)&&(key1==1)) {ledcount1 = 0;Pattern1();} //Preset 4 patterns
if((key2==0)&&(key1==1)) {ledcount1 = 0;Pattern2();}
if((key2==1)&&(key1==0)) {ledcount = 0;Gra3 = 1;Pattern34();}
if((key2==0)&&(key1==0)) {ledcount = 0;Gra7 = 1;Pattern34();}
}
void value()
{
if(Speed == 20) PWM = Pulse*8;
if(Speed == 50) PWM = Pulse*4; //Number of pulsesif
(Speed == 100) PWM = Pulse*2;
if(Speed == 150) PWM = Pulse;
}
void Reset(void)
{
unsigned char code rst[ ]={0xe4,0xc0,0xe0,0xc0,0xe0,0x32};
(*((void (*)(void))(rst)))();
}
void main()
{
_nop_ ();
_nop_ ();
_nop_ ();
P1 = 0;
TMOD = 0x11; //11.0592M crystal oscillator 10ms timing initial value (one hundredth of a second)
TH0 = 0x0db;
TL0 = 0x0ff;
TH1 = 0x0ff; //0.05ms(1/20000 of a second)
TL1 = 0x0D1;
EA = 1; //Enable total interrupt
ET0 = 1; //Enable T0 timer interrupt
ET1 = 1; //Enable T1 timer interrupt
TR0 = 1; //Enable T0 timer
EX0=1; //Open external interrupt 0
IT0=1; //Falling edge triggerwhile
(1)
{
keyscan();
}
}
//*****************************************************************************
//Pattern1() Pattern function 1-------------Jumping without turning off the light process
//*************************************************************************
void Pattern1()
{
//keyscan();
Gra = 0; //Jumping bit = 0
if(LEDdelay)
{
LEDdelay = 0;
P1 = led1[ledcount];
ledcount++;
if(ledcount>11)
{
ledcount = 0;
}
}
}
//***************************************************************************
//Pattern2() Pattern function 2-------------Jumping with light-off process
//***********************************************************************
void Pattern2()
{
//keyscan();
Gra = 0;
if(LEDdelay)
{
LEDdelay = 0;
P1 = led2[ledcount];
ledcount++;
if(ledcount>23)
{
ledcount = 0;
}
}
}
//***************************************************************************
//Pattern3() Pattern function 3-------------3-color gradient
//***************************************************************************
/*void Pattern3()
{
keyscan();
if(Speed == 20) Period = 25;
if(Speed == 50) Period = 50;
if(Speed == 100)Period = 100;
if(Speed == 150)Period = 200;
Gra = 1;
Gra3 = 1;
}*/[page]
//***************************************************************************
//Pattern4() Pattern function 4-------------7-color gradient
//*******************************************************************
void Pattern34()
{
Gra = 1; //Gradient bit = 1
}
/*************************************************************/
// Timer 0 interrupt service routine
/*********************************************************/
void time0(void) interrupt 1
{
TR1 =0;
TF0 =0;
TH0=0xdb; //Rewrite the initial value
TL0=0xff;
if(!Gra) //Execute the jump program
{
count ++;
if(count > Speed)
{
LEDdelay = 1;
count = 0;
}
}
if(Gra)
{
TR1 = 1; //Turn on TR1;
if(!BP) //
{
if(Gra7)
{
P1 = led1[ledcount1]; //10ms arrives, the corresponding LED turns on
}
if(Gra3)
{
P1 = led3[ledcount1]; //10ms arrives, the corresponding LED turns on
}
Pulse++;
value();
if(Pulse >= Period)
{
Pulse = Period;
BP = 1; //One cycle completes the conversion mode
}
}
if(BP)
{
if(Gra7)
{
P1 = led1[ledcount1]; //10ms arrives, the corresponding LED turns on
}
if(Gra3)
{
P1 = led3[ledcount1]; //10ms arrives, the corresponding LED turns on
}
value();
Pulse--; //Number of pulsesif
(Pulse == 0)
{
Pulse = 0;
BP = 0; //One cycle completes the conversion mode
ledcount1++;
if(Gra7)
{
if(ledcount1 > 11)
{
ledcount1 = 0;
}
Gra7 = 0;
}
if(Gra3)
{
if(ledcount1 > 2)
{
ledcount1 = 0;
}
Gra3 = 0;
}
}
}
}
}
/****************************************************************/
// Timer 1 interrupt service routine
/********************************************************/
void timer1() interrupt 3
{
PWM --;
TH1 = 0xff; //0.05ms (one-twenty-thousandth of a second)
TL1 = 0xD1;
if(PWM == 0)
{
P1 = 0;
TR1 = 0;
}
}
/************************************************************/
// External interrupt 0 interrupt service routine falling edge trigger
/********************************************************/
void extern0(void) interrupt 0
{
IE0 = 0; // Clear flag
excount++;
if(excount>50) // 50 is about 3 seconds
{
EA = 0;
//P1_5 = 1;
excount = 0;
Reset();
}
}
Previous article:Test digital tube display numbers (single chip microcomputer directly realizes bit selection common cathode)
Next article:Elevator control program based on single chip microcomputer
- Popular Resources
- Popular amplifiers
- Chip Manufacturing: A Practical Tutorial on Semiconductor Process Technology (Sixth Edition)
- P89LPC915: 8-bit microcontroller based on 80C51 core
- Design of parameter tester based on C8051F005 single chip microcomputer
- Multi-channel pressure measuring instrument based on C8051F020 single chip microcomputer
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Experiment of controlling relay with MF RC522 RF card based on Arduino UNO R3
- Problems in transplanting UCOSII to 51 MCU
- LiChuang Mall is right and confident in its mistakes. Everyone should be careful and don't be cheated.
- The CC2530 low-power terminal keeps reconnecting to the network and consumes power
- Crazy Shell AI open source drone OPENMV script burning
- For C5000, how to boot a program larger than 48K?
- Qorvo's technical highlights on WiFi 6
- Introduction to the mobile station development board: STM32G071 NUCLEO-64
- How to locate RFID based on wireless network
- Regarding the issue of using NPN transistors for high voltage conversion output.