本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
用c8051f310产生两路spwm波做正弦逆变 可带两盏400w的高压钠灯 可实现与上位机的通讯 采样 完成调节电压 控制灯亮灭与定时开关灯等等功能。现摘录控制器中spwm产生程序如下:
#include <C8051F310.h> // SFR declarations
#define uint unsigned int
#define uchar unsigned char
sfr16 TMR2RL= 0xCA; // Timer 2 Reload定义SPWM数据
sfr16 TMR2= 0xCC; // Timer 2 Counter
#define FREQUEN 24500000 //SYSCLK frequency in Hz
#define SYSCLK FREQUEN //
#define SPWMFREQ 50 //定义SPWM频率
#define ON 1
#define OFF 0
#define SEC 1 //秒
#define MIN 60*SEC //分
#define HOU 60*MIN //时
#define DATA_LEN 96
#define M_PI 3.14159265358979323846
/*********************Timer1用作通用计时器********************************/
#define TIMER1_RELOAD -SYSCLK/12 /2/9600 //设置波特率为9600
#define TIMER1_RELOAD_HIGH TIMER1_RELOAD
#define TIMER1_RELOAD_LOW TIMER1_RELOAD
/*********************Timer2用作SPWM定时触发********************************/
#define TIMER2_BASE SYSCLK /12/SPWMFREQ/64; // 初始化重载值
#define TIMER2_RELOAD -TIMER2_BASE //计数10ms
#define TIMER2_RELOAD_HIGH ((TIMER2_RELOAD&0xFF00)>>8)
#define TIMER2_RELOAD_LOW TIMER2_RELOAD&0xFF
/*********************Timer3用作ADC0定时触发********************************/
#define TIMER3_BASE SYSCLK /SPWMFREQ/96; // 初始化重载值
#define TIMER3_RELOAD -TIMER3_BASE //计数10ms
#define TIMER3_RELOAD_HIGH ((TIMER3_RELOAD&0xFF00)>>8)
#define TIMER3_RELOAD_LOW TIMER3_RELOAD&0xFF
uchar code table[64]={0,25,50,74,98,120,142,162,
180,197,212,225,235,244,250,252,255,
252,250,244,235,225,212,197,180,162,142,
120,98,74,50,25,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0};
sbit SPWM0=P0^0; //PWM0 output pin
sbit SPWM1=P0^1; //PWM1 output pin
void Init_SYS (void);//
void SYSCLK_Init (void); //时钟源初始化函数
void PORT_Init (void); // 端口初始化
void PCA_Init (void); //PCA初始化
void delay(uint time); //延时函数
void Timer2_Init (void); //给定时器2设初值
void Timer0_Init(void);
//void Timer1_Init();
//void ADC0_Init(void);
//void Timer3_Init(void);
//void UART0_Init(void);
//void Modbus_Init(void);
//void Read_ADDR(void);
void Init_SYS (void)
{
ADDR=0;
US=220;
PCA0MD = 0x00; // 系统时钟为FOS/12,空闲方式PCA继续工作,关闭看门狗
PORT_Init (); // 端口初始化
SYSCLK_Init (); //时钟源初始化函数
Timer2_Init (); //给定时器2设初值
Timer0_Init();
// Timer1_Init();
//Timer3_Init();
//UART0_Init();
PCA_Init (); //PCA初始化
// ADC0_Init();
// Modbus_Init();
delay(10);
IP=0X20; //Timer2为高优先级中断 UART0为低优先级中断
//开总中断
// Read_ADDR();
// ADDR=0X01;
VDM0CN = 0x80; // enable VDD monitor
RSTSRC |= 0x02; // enable VDD monitor as a reset source
EA=1;
// ZD_Xz_Flg=1;
}
//----------------------------------------------------------------------------
//时钟源初始化函数
//-----------------------------------------------------------------------------
void SYSCLK_Init (void) //时钟源初始化函数,保证工作与24500000/1
{
int i;
OSCXCN = 0x67; // 使能外部晶振,24。576M
for(i=0;i<30000;i++); // 切换到外部晶振前,等待1ms,
while (!(OSCXCN & 0x80)) ; // 等待外部晶振稳定
RSTSRC = 0x02; // 丢失时钟重启,复位
CLKSEL = 0x01; // 选择外部晶振为系统时钟
OSCICN = 0x00;
}
//-----------------------------------------------------------------------------
// 端口初始化
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void PORT_Init (void) // 端口初始化
{
P1MDIN=0Xf0;
P1MDOUT = 0xF0;
P1=0X00;
P0MDIN = 0xF3;
P0MDOUT |= 0xF3; // 设置P0.0,P0.1为推挽输出
P0SKIP |= 0X0C;
P1SKIP = 0x0F;
XBR0 = 0x01;
XBR1 = 0x42; // 设置 CEX0, CEX1 到 P0.0,P0.1
// 使能交叉开关
}
//-----------------------------------------------------------------------------
// Timer0 初始化
//--------------------------------------------------- --------------------------
void Timer0_Init() //time0用来确定spwm的频率
{
TMOD = 0x02;
CKCON |= 0x04;
TL0 = 0xF6;
TH0 = 0xF6;
TR0=1;
}
void Timer1_Init() //通讯波特率
{
TMOD |= 0X20;
CKCON &=~0X08; // Timer1 uses SYSCLK/12
TH1 =TIMER1_RELOAD_HIGH;
TL1 =TIMER1_RELOAD_LOW;
TR1 =1;
}
void Timer2_Init (void)
{
CKCON &= ~0x60; // Timer2 uses SYSCLK/12
TMR2CN &= ~0x01;
TMR2RL =TIMER2_RELOAD; // 初始化重载值
TMR2 = TMR2RL; // Init the Timer2 register
TR2 = 1; //定时器2运行控制
ET2 = 1; //开定时器2中断
}
//-----------------------------------------------------------------------------
void PCA_Init (void)
{
PCA0CN = 0x00;
PCA0MD = 0x04;
PCA0CPM0 = 0x42; // 模块0 8 位 PWM 模式
PCA0CPM1 = 0x42; // 模块1 8 位 PWM 模式
CR = 1;
}
//-----------------------------------------------------------------------------
// *************系统 main()函数**************************___________________//
//-----------------------------------------------------------------------------
void main (void)
{
Init_SYS();
// Flash_Read(0x0000,10,FlashBufRD);
// _nop_();
// Flash_Eraser(0x3000);
// _nop_();
// Flash_Write(0x3000,10,FlashBufWR);
// _nop_();
// Flash_Eraser(3000);
//Flash_Write (3000,7, syss_con_temp);
while(1)
{
// ADC_main();
// Rx_Yc ();
// Change_us();
// Aut_Adjust_Process();
}
}
/*************End for interrupt of Timer2**************/
/****************interrupt of Timer2*************/
void TIMER2_ISR (void) interrupt 5
{
uchar a,b;
uint m,n;
TF2H = 0;
m=(uint)table[con0&0x3f]*SpwmValue;
n=(uint)table[con1&0x3f]*SpwmValue;
a= m/255;
b= n/255;
if(a==0XFF) PCA0CPM0 &= ~0x40;
else PCA0CPH0=a;
if(b==0XFF) PCA0CPM1 &= ~0x40;
else PCA0CPH1=b;
con0++;
con1++;
} |