#include<pic.h>
//单片机配置字,详见手册
__CONFIG(0x3b32);
//函数原型
//*********************************************************************************
unsigned int get_AD_CH0();
unsigned int get_AD_CH1();
unsigned int get_AD_CH2();
unsigned int get_AD_CH3();
unsigned int get_AD_CH4();
unsigned int get_AD_CH5();
unsigned int get_AD_CH6();
unsigned int get_AD_CH7();
void PWM_CH1(unsigned int PWM);
void PWM_CH2(unsigned int PWM);
//单片机设备初始化函数
void globle_initialize(void); //全局初始化设置函数
void serial_port_initialize(void); //串行口初始化设置函数
void PWM_initialize();
//单片机中断函数
void interrupt IRQ(void); //单片机中断函数
//函数原型
unsigned char leng =0;
unsigned char tf =0;
unsigned char TxBuf=0; // 8字节, 如果需要更长的数据包,请正确设置
unsigned char RxBuf=0;
//*********************************************************************************
//*****************************************************************************************
//函数名:delay(unsigned int s)
//输入:时间
//输出:无
//功能描述:普通廷时,内部用
//*****************************************************************************************
static void delay(unsigned int s)
{
unsigned int i;
for(i=0; i<s; i++);
for(i=0; i<s; i++);
}
//全局初始化设置
void globle_initialize()
{
//端口方向设置
TRISA=0b11111111; //端口A方向设置0为输出,1为输入
TRISB=0b00000000; //端口B方向设置0为输出,1为输入
TRISC=0b00000000; //端口C方向设置0为输出,1为输入
TRISD=0b00000000; //端口D方向设置0为输出,1为输入
RC0=1;
//端口方向设置
//中断设置
//INTCON 寄存器 见P24
GIE=1; //全局中断开关
PEIE=1; //外设中断开关
//中断设置
}
////////////////////////////////////////////////////////////////////
//串行口初始化设置
void serial_port_initialize()
{
//串行口工作于异步方式
SYNC=0;
CREN=1;
BRGH=1;//高波特率方式:Baud Rate = Fosc/(16*(SPBRG+1))
TXEN=1;
TX9=0;//发送数据长度为8位
RX9=0;//接收数据长度为8位
SPBRG=129;// 通信波特率为9600
SPEN=1;
//TXIE=1;//SCI串行口发送中断允许
RCIE=1;//SCI串行口接收中断允许
TRISC7=1;
}
void PWM_initialize()
{
TRISC1=0; //CCP2
TRISC2=0; //CCP1
PR2=0xff;//设置Timer2周期
T2CON=0b01111011; //Timer2 设置
//CCP2CON=0x0f;//将CCP2模块设置为PWM模式
TMR2ON=1; //Timer2 开
}
void PWM_CH1(unsigned int PWM)
{
CCP1CON=0x00;//关闭CCP1
CCPR1L=PWM;
CCP1CON=0x0f;//将CCP1模块设置为PWM模式
}
void PWM_CH2(unsigned int PWM)
{
CCP2CON=0x00;//关闭CCP2
CCPR2L=PWM;
CCP2CON=0x0f;//将CCP2模块设置为PWM模式
}
unsigned int get_AD_CH0() //电池板电压 solar_V
{
unsigned int value;
PORTA=0;
ADCON0=0b10000001;
ADCON1=0b00000000;
delay(1000);
ADGO=1;
while(ADGO);
value=ADRESH;
ADCON0=0b10000000;
return (value);
}
unsigned int get_AD_CH1() //电池板电流 solar_I
{
unsigned int value;
PORTA=0;
ADCON0=0b10001001;
ADCON1=0b00000000;
delay(1000);
ADGO=1;
while(ADGO);
value=ADRESH;
ADCON0=0b10000000;
return (value);
}
unsigned int get_AD_CH2() //电池电压 battary_V
{
unsigned int value;
PORTA=0;
ADCON0=0b10010001;
ADCON1=0b00000000;
delay(1000);
ADGO=1;
while(ADGO);
value=ADRESH;
ADCON0=0b10000000;
return (value);
}
unsigned int get_AD_CH3() //电池电流 battary_I
{
unsigned int value;
PORTA=0;
ADCON0=0b10011001;
ADCON1=0b00000000;
delay(1000);
ADGO=1;
while(ADGO);
value=ADRESH;
ADCON0=0b10000000;
return (value);
}
unsigned int get_AD_CH4()
{
unsigned int value;
PORTA=0;
ADCON0=0b10100001;
ADCON1=0b00000000;
delay(1000);
ADGO=1;
while(ADGO);
value=ADRESH;
ADCON0=0b10000000;
return (value);
}
unsigned int get_AD_CH5()
{
unsigned int value;
PORTA=0;
ADCON0=0b10101001;
ADCON1=0b00000000;
delay(1000);
ADGO=1;
while(ADGO);
value=ADRESH;
ADCON0=0b10000000;
return (value);
}
unsigned int get_AD_CH6()
{
unsigned int value;
PORTA=0;
ADCON0=0b10110001;
ADCON1=0b00000000;
delay(1000);
ADGO=1;
while(ADGO);
value=ADRESH;
ADCON0=0b10000000;
return (value);
}
unsigned int get_AD_CH7()
{
unsigned int value;
PORTA=0;
ADCON0=0b10111001;
ADCON1=0b00000000;
delay(1000);
ADGO=1;
while(ADGO);
value=ADRESH;
ADCON0=0b10000000;
return (value);
}
unsigned int average_AD_CH0()//平均电池板电压 solar_Va
{
unsigned int value=0,i;
PORTA=0;
ADCON0=0b10000001;
ADCON1=0b00000000;
delay(1000);
for (i=1; i<=32; ++i)
{
ADGO=1;
while(ADGO);
value=value+ADRESH;
delay(1000);
}
ADCON0=0b10000000;
return(value/32);
}
unsigned int average_AD_CH1() //平均电池板电流 solar_Ia
{
unsigned int value=0,i;
PORTA=0;
ADCON0=0b10001001;
ADCON1=0b00000000;
delay(1000);
for (i=1; i<=32; ++i)
{
ADGO=1;
while(ADGO);
value=value+ADRESH;
delay(1000);
}
ADCON0=0b10000000;
return(value/32);
}
unsigned int average_AD_CH2() //平均电池电压 battary_Va
{
unsigned int value=0,i;
PORTA=0;
ADCON0=0b10010001;
ADCON1=0b00000000;
delay(1000);
for (i=1; i<=32; ++i)
{
ADGO=1;
while(ADGO);
value=value+ADRESH;
delay(1000);
}
ADCON0=0b10000000;
return(value/32);
}
unsigned int average_AD_CH3() //平均电池电流 battary_Ia
{
unsigned int value=0,i;
PORTA=0;
ADCON0=0b10011001;
ADCON1=0b00000000;
delay(1000);
for (i=1; i<=32; ++i)
{
ADGO=1;
while(ADGO);
value=value+ADRESH;
delay(1000);
}
ADCON0=0b10000000;
return(value/32);
}
unsigned int average_AD_CH4()
{
unsigned int value=0,i;
PORTA=0;
ADCON0=0b10100001;
ADCON1=0b00000000;
delay(1000);
for (i=1; i<=32; ++i)
{
ADGO=1;
while(ADGO);
value=value+ADRESH;
delay(1000);
}
ADCON0=0b10000000;
return(value/32);
}
unsigned int average_AD_CH5()
{
unsigned int value=0,i;
PORTA=0;
ADCON0=0b10101001;
ADCON1=0b00000000;
delay(1000);
for (i=1; i<=32; ++i)
{
ADGO=1;
while(ADGO);
value=value+ADRESH;
delay(1000);
}
ADCON0=0b10000000;
return(value/32);
}
unsigned int average_AD_CH6()
{
unsigned int value=0,i;
PORTA=0;
ADCON0=0b10110001;
ADCON1=0b00000000;
delay(1000);
for (i=1; i<=32; ++i)
{
ADGO=1;
while(ADGO);
value=value+ADRESH;
delay(1000);
}
ADCON0=0b10000000;
return(value/32);
}
unsigned int average_AD_CH7()
{
unsigned int value=0,i;
PORTA=0;
ADCON0=0b10111001;
ADCON1=0b00000000;
delay(1000);
for (i=1; i<=32; ++i)
{
ADGO=1;
while(ADGO);
value=value+ADRESH;
delay(1000);
}
ADCON0=0b10000000;
return(value/32);
}
void interrupt IRQ(void)
{
unsigned char temp,value;
//if(TXIF)//SCI串行口发送中断服务子程序
// {
// TXIF=0;
// }
if(RCIF) //SCI串行口接收中断服务子程序
{
//while(tf==1);
RCIF=0;
temp=RCREG;
PWM_CH1(temp);
TXREG=temp;
while(!TRMT);
}
}
void main()
{
unsigned int pwm;
RD3=0; //lt1158 关
globle_initialize(); //全局初始化设置函数
serial_port_initialize(); //串行口初始化设置函数
PWM_initialize();
// RB3=1;
RD3=1; //lt1158 开
//pwm=0xff;
//PWM_CH1(pwm);
// delay(10000);
// PORTD=0xff;
// PORTB=0xff;
while(1)
{ // loop forever
}
}