8-way PWM function waveform output program can be controlled separately according to the script

Publisher:深沉思考Latest update time:2015-01-16 Source: 51heiKeywords:Script Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 

 
[page]

Detailed production process: http://www.51hei.com/bbs/dpj-25725-1.html 
Here is the source code:
#include
//#include
 
#define ONE_CYCLE_STEP 255
 
#ifdef AT89C2051_HEADER_FILE
#define PORT P1 //If using STC12C2052, use P1 as the output port
#else
#define PORT P0 //Otherwise use P0 port for output
#endif
 
#define LED_CNT 4 //Define the output port number 1-8 4 represents the use of 0-3 port output
#define SCRIPT_CNT 6 //Define the script number
 
//Firefly structure
struct Firefly{
unsigned int brightness; //maximum brightness value 0-255
unsigned int timeCell; //Time interval 0-65535
unsigned int count; //Run control variable: current count
unsigned char bn; //Run control variable: current brightness
};
 
//Define the execution script for each output port
// Each port will execute SCRIPT_CNT times, and then continue to execute in a loop
struct Firefly code fireFlyScript[][SCRIPT_CNT] = {
{{0,300,0,0},{255,400,0,0},{0,500,0,0},{100,600,0,0},{0,900,0,0},{0,500,0,0}},
{{255,300,0,0},{0,400,0,0},{0,500,0,0},{255,800,0,0},{0,700,0,0},{0,500,0,0}},
{{0,300,0,0},{0,400,0,0},{255,500,0,0},{50,1200,0,0},{0,300,0,0},{0,500,0,0}},
{{128,500,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}},
{{0,900,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}},
{{255,400,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}},
{{0,200,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}},
{{0,750,0,0},{255,300,0,0},{50,200,0,0},{0,1000,0,0},{255,1500,0,0},{50,100,0,0}}
};
 
//define LED_CNT only fireflies
struct Firefly fireFlies[LED_CNT];
 
//Script counter for each firefly
unsigned char ffScript[LED_CNT];
 
//Pre-calculated cosine function table, PWM output simulation function curve is better
unsigned char code cosValue[]={
0,0,0,0,0,0,0,0,1,1,1,2,2,3,3,4,4,5,6,6,7,8,
9,10,11,11,12,13,14,16,17,18,19,20,21,23,24,
25,27,28,29,31,32,34,35,37,39,40,42,44,45,47,
49,50,52,54,56,58,60,62,63,65,67,69,71,73,75,
77,80,82,84,86,88,90,92,94,97,99,101,103,105,
107,110,112,114,116,119,121,123,125,127,130,
132,134,136,139,141,143,145,148,150,152,154,
156,158,161,163,165,167,169,171,173,175,178,
180,182,184,186,188,190,191,193,195,197,199,
201,203,205,206,208,210,211,213,215,216,218,
220,221,223,224,226,227,228,230,231,232,234,
235,236,237,238,239,241,242,243,244,244,245,
246,247,248,249,249,250,251,251,252,252,253,
253,254,254,254,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,254,254,254,
253,253,252,252,251,251,250,249,249,248,247,
246,245,244,244,243,242,241,239,238,237,236,
235,234,232,231,230,228,227,226,224,223,221,
220,218,216,215,213,211,210,208,206,205,203,
201,199,197,195,193,192,190,188,186,184,182,
180,178,175,173,171,169,167,165,163,161,158,
156,154,152,150,148,145,143,141,139,136,134,
132,130,128,125,123,121,119,116,114,112,110,
107,105,103,101,99,97,94,92,90,88,86,84,82,
80,77,75,73,71,69,67,65,64,62,60,58,56,54,52,
50,49,47,45,44,42,40,39,37,35,34,32,31,29,28,
27,25,24,23,21,20,19,18,17,16,14,13,12,11,11,
10,9,8,7,6,6,5,4,4,3,3,2,2,1,1,1,0,0,0,0,0,0,0,
0
};
 
//Timer interrupt count variable
unsigned int time0count_0;
 
//---------------------------------------
//Name: Timer 0 interrupt service routine
// :Timer0
//parameter:
//return:
//---------------------------------------
void Timer0(void) interrupt 1 
{                          
unsigned long cell; //temporary variable for calculation
unsigned char i,p;
TL0=0xE8; //Re-initialize TL0
TH0=0xFF; //Re-assign the initial value to TH0
 
//After a cycle ends, check the script and the duty cycle of the next cycle
if(++time0count_0>ONE_CYCLE_STEP)
{
time0count_0 = 0;
//N channels of signal processing
for(i = 0;i
{
//After one script is executed, load the next script
if(++fireFlies[i].count>fireFlies[i].timeCell)
{
ffScript[i]++;
ffScript[i] = ffScript[i] % SCRIPT_CNT;
fireFlies[i] = fireFlyScript[i][ffScript[i]];
}
//Calculate the duty cycle and brightness of the next cycle
cell = fireFlies[i].count;
cell *=360;
cell /= fireFlies[i].timeCell;
fireFlies[i].bn = (cosValue[cell]*fireFlies[i].brightness)/255;
//Prepare to light up an LED
p=1<
//If the brightness value is not 0, light up
if(fireFlies[i].bn != 0)
{
PORT |= p;
}
else
{
PORT &= ~p;
}
}
}
//After each output port reaches the duty cycle, it outputs a low level to turn off the LED
for(i = 0; i
{
p=1<
if(time0count_0==fireFlies[i].bn)PORT &= ~p;
}
}
 
void main()
{
unsigned i;
 
// Turn off all LEDs
PORT=0;
 
//Initialize all fireflies and execute scripts
for(i = 0; i
{
fireFlies[i] = fireFlyScript[i][0];
ffScript[i] = 0;
}
 
//***Timer 0 initialization***
TMOD&=0xF0; //Clear the lower 4 bits of TMOD timer 0 control part
TMOD|=0x01; //Set timer 0 to mode 1
TL0=0xE8; //Set the initial value of timer 0 to the lower 8 bits
TH0=0xFF; //Set the initial value of timer 0 high 8 bits
TR0=1; //Start timer 0
ET0=1; //Timer0 interrupt enabled
 
//***Open global interrupt settings****
//Timer Timer0 is set to enable interrupts, and global interrupts need to be enabled here
EA=1; //Open global interrupt
 
while(1); // infinite loop, after the microcontroller is initialized, it will keep running this infinite loop
}
Keywords:Script Reference address:8-way PWM function waveform output program can be controlled separately according to the script

Previous article:Traffic Light and Its Simulation Based on 51 Single Chip Microcomputer
Next article:51 MCU serial port control relay

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号