86% found this document useful (7 votes)
1K views3 pages

Pic 16 F 716

This document describes a PIC16F716 microcontroller program for controlling a motor using SPWM (sinusoidal pulse width modulation). It uses Timer1 to generate a 50Hz signal to control the motor speed. Timer2 generates a 50us SPWM signal to control motor direction. The program includes voltage and current protection by monitoring the ADC and a digital input pin.

Uploaded by

More Fun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
86% found this document useful (7 votes)
1K views3 pages

Pic 16 F 716

This document describes a PIC16F716 microcontroller program for controlling a motor using SPWM (sinusoidal pulse width modulation). It uses Timer1 to generate a 50Hz signal to control the motor speed. Timer2 generates a 50us SPWM signal to control motor direction. The program includes voltage and current protection by monitoring the ADC and a digital input pin.

Uploaded by

More Fun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

???PIC16F716??SPWM?,???????H?????????,?????????,???????

???
PIC16F716?PWM?????????,H???????????SPWM?(???????????),???
???????50HZ???timer2??????1:5,????40?????,SPWM????50US?????
while???????????????????????50HZ???????1ms????(?H??????
?????)??????,????,???~~

#include <pic.h>
#define LED_CTRL_PORT RA2 ////////////////
#define SD_OUT_PORT RB6 ////////////////
#define SD_IN_PORT RB0
#define LEFT_UP_CTRL_PORT RB1
#define LEFT_DOWN_CTRL_PORT RB2
#define RIGHT_UP_CTRL_PORT RB3
#define RIGHT_DOWN_CTRL_PORT RB5
#define HZ50_PORT_MASK 0x06 //RB1,RB2
#define SPWM_PORT_MASK 0x28 // RB5 ,RB3
#define SD_IN_PORT_MASK 0x01 // RB0
#define LED_PORT_MASK 0x04 // RA2
#define SD_OUT_PORT_MASK 0x40 //RB6 ////////////////
#define VOL_PROTECT_NUM 1000
#define CUR_PROTECT_NUM 500

#define FM_CTRL_PORT RB4

__CONFIG(0xFF72);
unsigned char LeftUpCtrlNum=0,LeftDownCtrlNum=1;
unsigned char VoltageProtect=0,CurrentProtect=0;
unsigned int PortectNum=0;
volatile unsigned char SinNum=0;
static unsigned char SinTab[]={8,24,39,54,68,83,96,110,122,134,145,

155,164,172,179,185,189,193,195,197,

197,195,193,189,185,179,172,164,155,

145,134,122,110,96,83,68,54,39,24,8};
void interrupt timer1()
{
if(TMR1IF==1)
{
TMR1IF=0;
TMR1H=0xEC;
TMR1L=0x77;
//50HZ ???? start
LeftUpCtrlNum++;
if(LeftUpCtrlNum==9)
{
LEFT_UP_CTRL_PORT=0;
}
else if(LeftUpCtrlNum==20)
{
LeftUpCtrlNum=0;
LEFT_UP_CTRL_PORT=1;
}
LeftDownCtrlNum++;
if(LeftDownCtrlNum==11)
{
LEFT_DOWN_CTRL_PORT=1;
}
else if(LeftDownCtrlNum==20)
{
LeftDownCtrlNum=0;
LEFT_DOWN_CTRL_PORT=0;
}
//50HZ ???? end
//??????? start
PortectNum++;
if(CurrentProtect)
{
if(PortectNum==CUR_PROTECT_NUM)
{
PortectNum=0;
LED_CTRL_PORT=~LED_CTRL_PORT;
FM_CTRL_PORT=~FM_CTRL_PORT;
}
}
if(VoltageProtect)
{
if(PortectNum==VOL_PROTECT_NUM)
{
PortectNum=0;
LED_CTRL_PORT=~LED_CTRL_PORT;
FM_CTRL_PORT=~FM_CTRL_PORT;
}
}
//??????? end
}
//?????
if(TMR2IF==1)
{
TMR2IF=0;
SinNum++;
if(SinNum>=40)
{
SinNum=0;
}
CCPR1L=SinTab[SinNum];
T2CON=0x24;
}
}
main(void)
{
unsigned int i;
unsigned char ADCResult;
INTCON=0;
TRISA=0xff;
// TRISB=0xff;
ADCON1=0x04;
ADCON0=0x81;
TRISA&=~(LED_PORT_MASK);
TRISB=SPWM_PORT_MASK | SD_IN_PORT_MASK ;
T1CON=0x00;
TMR1H=0xEC;
TMR1L=0x77;
TMR1IF=0;
T1CON=0x01;
PR2=0xf9;
CCP1CON=0x8C; //10001100;
CCPR1L=SinTab[0];
PWM1CON=0x19;
TMR2IF=0; //Timer2 ???????
T2CON=0x24; //0010 0100???1,???5,??timer2

PIE1=0x03;
INTCON=0xC0;
TRISB&=~SPWM_PORT_MASK;
FM_CTRL_PORT=0;
LED_CTRL_PORT=1;
SD_OUT_PORT=0;
LEFT_UP_CTRL_PORT=1;
LEFT_DOWN_CTRL_PORT=0;

while(1)
{
if(!VoltageProtect)
{
i=50;

while(i>0)
i--;
ADCON0|=0x04;
while(ADCON0&0x04);
ADCResult=ADRES;
//??????
if(ADCResult<0x28 || ADCResult >0x42)
{
PortectNum=0;
VoltageProtect=1;
FM_CTRL_PORT=1;
LED_CTRL_PORT=0;
SD_OUT_PORT=1;
TRISB |=(SPWM_PORT_MASK|HZ50_PORT_MASK);
}
}
//???????????
if(!VoltageProtect && !SD_IN_PORT)/////////////////////
{
PortectNum=0;
CurrentProtect=1;
FM_CTRL_PORT=1;
LED_CTRL_PORT=0;
SD_OUT_PORT=1;
TRISB |=(SPWM_PORT_MASK|HZ50_PORT_MASK);
}
};
}

You might also like