#include "system.h"
#include "delay.h"
#include "oled.h"
#include "lcd.h"
#define K3 PORTAbits.RA12
unsigned int buffer1; //定义CAN接收缓冲单元
unsigned int buffer2;
unsigned int buffer3;
unsigned int buffer4;
unsigned int ADC_Data,T;
//int T;
//char i;
unsigned int j = 5,k,s,m,n,X,Y,kk,ss,mm,nn;
void System_Init(void)
{
ADPCFG = 0xffff;
TRISBbits.TRISB8 = 0;//RB5 RB8 输出
IEC0 = 0x0000;
IEC1 = 0x0000;
IEC2 = 0x0000;
TRISAbits.TRISA12 = 1;//输入
TRISB = 0x0000;
TRISF = 0x0000;
}
void ADC_Init(void)
{
ADPCFG = 0xfff7;
TRISBbits.TRISB3 = 1;//输入
ADCON1 = 0x0000;
ADCON1bits.SSRC = 7;
ADCON1bits.ASAM = 1;
ADCON2 = 0x0000;//
ADCON3 = 0x1f03; //使用系统时钟,相邻两次采样之间的时间间隔为31Tad,Tad=2Tcy
ADCHS = 0x0003;////通道0负输入是Vref-,通道0正输入是AN3
ADPCFGbits.PCFG3 = 0;//AN3模拟模式,禁止端口读取输入,A/D采样引脚电压
ADCSSL = 0x0000;//不对输入引脚扫描
IPC2bits.ADIP = 7; //设置AD转换中断优先级为7
IEC0bits.ADIE = 1; //使能AD转换完成中断
ADCON1bits.ADON = 1; //启动ADC
}
void tlv5616_da(unsigned int da)
{
ADPCFG = 0xfff7;
delay_ms(1);
char i;
PORTBbits.RB2 = 0;//定义RB2为片选
PORTFbits.RF6 = 1;//定义RF6为SCK脚
PORTFbits.RF7 = 0;//定义RF7为FS脚
LCD_ShowNum(110,550,da,4,16);//显示发送的值
for(i=0;i<16;i++)
{
delay_ms(1);
PORTFbits.RF8=(da&0x8000)>>15;//定义RF为数据口
PORTFbits.RF6 = 0;//定义RF6为SCK脚
delay_ms(1);
da<<=1;
PORTFbits.RF6 = 1;//定义RF6为SCK脚
delay_ms(1);
}
i=0;da=0;
PORTBbits.RB2 = 1;//定义RB2为片选
PORTFbits.RF7 = 1;//定义RF7为FS脚
PORTFbits.RF6 = 0;//定义RF6为SCK脚
}
void CAN1_Init(void)
{
C1CTRLbits.REQOP = 0x4;
while (C1CTRLbits.OPMODE!=0x4){}
C1TX0CON = 0x0003;
C1TX0SID = 0xC019;
C1TX0EID = 0x3043;
C1TX0DLC = 0xCDC0;
C1RX0CON = 0x0000;
C1RX0SID = 0x1819;
C1RX0EID = 0x0343;
C1RX0DLC = 0xCC08;
C1RXF0SID = 0x1819;
C1RXF0EIDH = 0x0343;
C1RXF0EIDL = 0xCC00;
C1RXM0SID = 0x1ffe;
C1RXM0EIDH = 0x0fff;
C1RXM0EIDL = 0xfC00;
C1CTRLbits.CANCKS = 1;/
C1CFG1bits.SJW=0;
C1CFG1bits.BRP = 1;
C1CFG2 = 0x06b4;
C1CTRLbits.REQOP = 0x0;
while (C1CTRLbits.OPMODE!=0x0){}
return;
}
void CAN2_Init(void)
{
//can set
C2CTRLbits.REQOP = 0x4;
while (C2CTRLbits.OPMODE!=0x4){}
C2TX0CON = 0x0003;
//C2TX0SID = 0x0020;
C2TX0SID = 0x60f1;
C2TX0EID = 0x000C;
C2TX0DLC = 0x01c0;
C2RX0CON = 0x0000;
C2RXF0SID = 0x0cf1;
C2RXF0EIDH = 0x000C;
C2RXF0EIDL = 0x0000;
C2RX0SID = 0x0cf1;
C2RX0EID = 0x000C;
C2RX0DLC = 0x0008;
C2RXM0SID = 0x1ffe;
C2RXM0EIDH = 0x0fff;
C2RXM0EIDL = 0xfC00;
C2CTRLbits.CANCKS = 1;
C2CFG1bits.SJW=0;
C2CFG1bits.BRP = 1;
C2CFG2 = 0x06b4;
C2CTRLbits.REQOP = 0x0;
while (C2CTRLbits.OPMODE!=0x0){}
return;
}
int main()
{
unsigned int V1;
System_Init();
LCD_Init();
LCD_Clear(RED);//背景 {MOD}
POINT_COLOR = BLACK;//设置字体为黑 {MOD}
LCD_ShowString(40,50,200,16,16,"CAN1_TX: ");
LCD_ShowString(40,80,200,16,16,"CAN2_RX: ");
LCD_ShowString(40,320,200,16,16,"ADC_Data: mV");
CAN1_Init();
CAN2_Init();
ADC_Init();
while(1)
{
if(C1RX0CONbits.RXFUL==1) //接收到数据
{
buffer1 = C1RX0B1; //读取接收到的数据
buffer2 = C1RX0B2;
buffer3 = C1RX0B3;
buffer4 = C1RX0B4;
C1RX0CONbits.RXFUL = 0;
kk = buffer1;
ss = buffer2;
mm= buffer3 ;
nn= buffer4 ;
LCD_ShowNum(110,380,kk,4,16);//显示发送的值
LCD_ShowNum(110,3410,ss,4,16);//显示发送的值
LCD_ShowNum(110,450,mm,4,16);//显示发送的值
LCD_ShowNum(110,480,nn,4,16);//显示发送的值
}
if(C2RX0CONbits.RXFUL==1) //接收到数据
{
buffer1 = C2RX0B1; //读取接收到的数据
buffer2 = C2RX0B2;
buffer3 = C2RX0B3;
buffer4 = C2RX0B4;
C2RX0CONbits.RXFUL = 0;
k = buffer1;
s = buffer2;
m= buffer3 ;
n= buffer4 ;
LCD_ShowNum(110,80,k,4,16);//显示发送的值
LCD_ShowNum(110,110,s,4,16);//显示发送的值
LCD_ShowNum(110,140,m,4,16);//显示发送的值
LCD_ShowNum(110,170,n,4,16);//显示发送的值
}
tlv5616_da(k);
delay_ms(50);
V1 = ADC_Data*3.3/4095 * 1000;
LCD_ShowNum(110,320,V1,4,16);//显示ADC的值
delay_ms(100);
PORTBbits.RB8 = 0;//RB8高电平输出
delay_ms(100);
PORTBbits.RB8 = 1;//RB8低电平输出
delay_ms(100);
}
}
void __attribute__((interrupt, no_auto_psv)) _ADCInterrupt (void)
{
IFS0bits.ADIF=0; //清AD转换中断标志位
ADC_Data = ADCBUF0;
}
CAN传输进来的值TLV5616不工作,如果吧k改为数值比方说1000,程序工作正常。请各位帮我看看。先说声谢谢!
此帖出自
小平头技术问答
一周热门 更多>