专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

PIC单片机16F716产生SPWM程序和仿真

作者:佚名   来源:本站原创   点击数:  更新时间:2014年08月09日   【字体:

是pic单片机做的完整代码和仿真文件下载地址: http://www.51hei.com/bbs/dpj-26880-1.html   可以产生spwm波形
#include "main.h"

#include "pic16f716.h"
 
#define _BV(N) (1<<N)
 
void Port_Init(void)
{
set_tris_b(0x00);
}
 
void T2PWM_OutPut(unsigned int16 t2_pwm)
{
unsigned int8 value = 0;
T2CON &= ~_BV(2);
value = CCP1CON;
value |= (unsigned int8)((t2_pwm&0x03)<<4);
CCP1CON = value;
CCPR1L = (unsigned int8)(t2_pwm>>2);
T2CON |= _BV(2);
}
 
void PWM_Init(void)
{
PWM1CON = 0b01100100;
#if 0
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4,127,1);
set_pwm1_duty(408);
#else
TRISB = 0x00;
PR2 = 249;
CCP1CON = 0b10001110;
//CCPR1L = 0x00;
T2PWM_OutPut(512);
TMR2 = 0x00;
TMR2IF = 0;
T2CON = 0b00000101;
while(TMR2IF==0);
TRISB = 0x00;
/*
TRISB = 0xFF;
PR2 = 249;
CCP1CON = 0b00001100;
//CCPR1L = 0x00;
T2PWM_OutPut(512);
TMR2 = 0x00;
TMR2IF = 0;
T2CON = 0b00000101;
while(TMR2IF==0);
TRISB = 0x00;
*/
#endif
}
 
void main(void)
{
Port_Init();
PWM_Init();
//for(;;)
while (TRUE)
{
//output_b(0x55);
delay_ms(200);
//output_b(0xaa);
delay_ms(200);
}
}
 
关闭窗口

相关文章