当前位置:网站首页>SCM signal generator program
SCM signal generator program
2022-06-13 03:54:00 【DC-STDIO】
List of articles
MCU signal generator program
With D/A This weapon , We can not only output square wave signals , You can output any waveform , Like sine wave 、 Triangle wave 、 Sawtooth wave, etc . Take sine wave as an example , First we need to build a sine wave table . These do not need to be calculated one by one , The sine wave data table can be found by searching , Then we can choose a certain amount of data as the sine wave meter of our program according to the time parameters , Our program code selects 32 A little bit .
#include <reg52.h>
unsigned char code SinWave[] = {
// Sine wave meter
127, 152, 176, 198, 217, 233, 245, 252,
255, 252, 245, 233, 217, 198, 176, 152,
127, 102, 78, 56, 37, 21, 9, 2,
0, 2, 9, 21, 37, 56, 78, 102
};
unsigned char code TriWave[] = {
// Triangular wave table
0, 16, 32, 48, 64, 80, 96, 112,
128, 144, 160, 176, 192, 208, 224, 240,
255, 240, 224, 208, 192, 176, 160, 144,
128, 112, 96, 80, 64, 48, 32, 16
};
unsigned char code SawWave[] = {
// Sawtooth wave meter
0, 8, 16, 24, 32, 40, 48, 56,
64, 72, 80, 88, 96, 104, 112, 120,
128, 136, 144, 152, 160, 168, 176, 184,
192, 200, 208, 216, 224, 232, 240, 248
};
unsigned char code *pWave; // Wave meter pointer
unsigned char T0RH = 0; //T0 High byte of overloaded value
unsigned char T0RL = 0; //T0 The low byte of the overloaded value
unsigned char T1RH = 1; //T1 High byte of overloaded value
unsigned char T1RL = 1; //T1 The low byte of the overloaded value
void ConfigTimer0(unsigned int ms);
void SetWaveFreq(unsigned char freq);
extern void KeyScan();
extern void KeyDriver();
extern void I2CStart();
extern void I2CStop();
extern bit I2CWrite(unsigned char dat);
void main(){
EA = 1; // General interruption
ConfigTimer0(1); // To configure T0 timing 1ms
pWave = SinWave; // Default sine wave
SetWaveFreq(10); // Default frequency 10Hz
while (1){
KeyDriver(); // Call the key driver
}
}
/* Key action function , Perform the corresponding operation according to the key code ,keycode- Key code */
void KeyAction(unsigned char keycode){
static unsigned char i = 0;
if (keycode == 0x26){
// Up key , Switching waveforms
// stay 3 Cyclic switching between waveforms
if (i == 0){
i = 1;
pWave = TriWave;
}else if (i == 1){
i = 2;
pWave = SawWave;
}else{
i = 0;
pWave = SinWave;
}
}
}
/* Set up DAC Output value ,val- The set value */
void SetDACOut(unsigned char val){
I2CStart();
if (!I2CWrite(0x48<<1)){
// Addressing PCF8591, If not answered , Stop the operation and return to
I2CStop();
return;
}
I2CWrite(0x40); // Write control byte
I2CWrite(val); // write in DA value
I2CStop();
}
/* Set the frequency of the output waveform ,freq- Set the frequency */
void SetWaveFreq(unsigned char freq){
unsigned long tmp;
tmp = (11059200/12) / (freq*32); // The timer counts the frequency , Is the waveform frequency 32 times
tmp = 65536 - tmp; // Calculate timer overload value
tmp = tmp + 33; // Correct the error caused by interrupt response delay
T1RH = (unsigned char)(tmp>>8); // The timer overload value is split into high and low bytes
T1RL = (unsigned char)tmp;
TMOD &= 0x0F; // Zero clearing T1 Control bit of
TMOD |= 0x10; // To configure T1 For mode 1
TH1 = T1RH; // load T1 Overload value
TL1 = T1RL;
ET1 = 1; // Can make T1 interrupt
PT1 = 1; // Set to high priority
TR1 = 1; // start-up T1
}
/* Configure and start T0,ms-T0 Timing time */
void ConfigTimer0(unsigned int ms){
unsigned long tmp; // Temporary variable
tmp = 11059200 / 12; // The timer counts the frequency
tmp = (tmp * ms) / 1000; // Calculate the required count
tmp = 65536 - tmp; // Calculate timer overload value
tmp = tmp + 28;// Compensate for the error caused by interrupt response delay
T0RH = (unsigned char)(tmp>>8); // The timer overload value is split into high and low bytes
T0RL = (unsigned char)tmp;
TMOD &= 0xF0; // Zero clearing T0 Control bit of
TMOD |= 0x01; // To configure T0 For mode 1
TH0 = T0RH; // load T0 Overload value
TL0 = T0RL;
ET0 = 1; // Can make T0 interrupt
TR0 = 1; // start-up T0
}
/* T0 Interrupt service function , Perform key scan */
void InterruptTimer0() interrupt 1{
TH0 = T0RH; // Reload overloaded values
TL0 = T0RL;
KeyScan(); // Key scan
}
/* T1 Interrupt service function , Execute waveform output */
void InterruptTimer1() interrupt 3{
static unsigned char i = 0;
TH1 = T1RH; // Reload overloaded values
TL1 = T1RL;
// Circularly output the data in the wave table
SetDACOut(pWave[i]);
i++;
if (i >= 32){
i = 0;
}
}
This program can be passed through “ Up ” Press the key to switch the waveform output , The timing refresh of waveform output is controlled by timer T1 Time to complete , change T1 The timing period can change the output frequency of the waveform .D/A The output cannot be connected to the display interface , So we use an oscilloscope to grab the waveform and show it to you
These figures can visually see the waveform output by our program . Careful students will find that there are many small serrations on our waveform , There is no smooth connection . It's because of us DA At most, it can only output 0~Vref Between 256 Discrete voltage values , Instead of continuous arbitrary values , So each discrete value will last for a certain time , Then jump to the next discrete value , So it shows the sawtooth on the waveform . In actual development , All we need to do is DA The next stage plus a low-pass filter circuit , You can make the sawtooth waveform smooth .
边栏推荐
猜你喜欢
随机推荐
LVS四層負載均衡集群(3)集群功能分類 - HPC
Very simple installation and configuration of nodejs
Installing MySQL 8.0.20 under Linux and ubuntu20.04 LTS
史上最详细的Swin-Transformer 掩码机制(mask of window attentation)————shaoshuai
Alibaba cloud keep on record
单片机:RS485 通信与 Modbus 协议
大五人格学习记录
干预分析 + 伪回归
Lambda end operation find and match allmatch
Multithreaded chat room, server and client
MCU: NEC protocol infrared remote controller
[note]vs2015 compilation of masm32 using MASM32 Library
Common array methods in JS (map, filter, foreach, reduce)
【测试开发】测试的相关基本概念
UDP connection map collection
[test development] automated test selenium (III) -- unittest framework analysis
[web] cookies and sessions
【 développement d'essais 】 sélénium d'essais automatisés (Ⅲ) - - analyse du cadre unitest
单片机/嵌入式的实时性疑问解答
单片机:Modbus 多机通信程序设计