專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

PIC單片機(jī)16F716產(chǎn)生SPWM程序和仿真

作者:佚名   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年08月09日   【字體:

是pic單片機(jī)做的完整代碼和仿真文件下載地址: http://www.torrancerestoration.com/bbs/dpj-26880-1.html   可以產(chǎn)生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);
}
}
 
關(guān)閉窗口

相關(guān)文章