一. 74hc165.h
#ifndef _74HC165_H
#define _74HC165_H
#include "sys.h"
#include "delay.h"
#include "rs485.h"
#include "can.h"
#define HC165_LD PAout(4)
#define HC165_CLK PAout(5)
#define HC165_QH PAin(6)
void _74hc165_init(void);
void _74hc165_read_3byte(u8 date[3]);
void s1u1_24(void);
#endif
二. 74hc165.c
#include "74hc165.h"
void _74hc165_init(void)
{
RCC->APB2ENR|=1<<2;
GPIOA->CRL&=0XF000FFFF;
GPIOA->CRL|=0X08880000;
HC165_LD = 1;
HC165_CLK = 1;
}
u8 i;
int temp;
void _74hc165_read_3byte(u8 date[3])
{
HC165_LD = 0;
delay_us(10);
HC165_LD = 1;
for(i = 0; i < 24; i++)
{
temp <<= 1;
HC165_CLK = 0;
delay_us(10);
if(HC165_QH == 1)
temp |= 0x01;
HC165_CLK = 1;
}
date[0] = (u8)(temp & 0x00ff);
date[1] = (u8)((temp >> 8) & 0x00ff);
date[2] = (u8)((temp >> 16) & 0x00ff);
temp=0;
delay_ms(100);
}
三. main.c
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "can.h"
#include "74hc165.h"
int main(void)
{
u8 key2state[3]={0xFF,0xFF,0xFF};
Stm32_Clock_Init(9);
delay_init(72);
_74hc165_init();
CAN_Mode_Init(1,8,9,4,0);
printf("RUN! \r\n");
while(1)
{
delay_ms(200);
_74hc165_read_3byte(key2state);
CAN_Send_Msg_Hex(key2state,3);
}
}
四. can.h
#ifndef __CAN_H
#define __CAN_H
#include "sys.h"
#define CAN_ENABLE PAout(15)
#define CAN_RX0_INT_ENABLE 1
extern u16 rpm;
extern int time;
u8 CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode);
u8 CAN_Tx_Msg(u32 id,u8 ide,u8 rtr,u8 len,u8 *dat);
u8 CAN_Msg_Pend(u8 fifox);
void CAN_Rx_Msg(u8 fifox,u32 *id,u8 *ide,u8 *rtr,u8 *len,u8 *dat);
u8 CAN_Tx_Staus(u8 mbox);
u8 CAN_Send_Msg(u8* msg,u8 len);
u8 CAN_Send_Msg_Hex(u8* msg,u8 len);
u8 CAN_Receive_Msg(u8 *buf);
u8 CAN_Tx_Msg_u16(u32 id,u8 ide,u8 rtr,u8 len,u16 *dat);
u8 CAN_Send_Msg_Hex_u16(u16* msg,u8 len);
#endif
五. can.c
#include "can.h"
#include "usart.h"
#include "delay.h"
u8 CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
u16 i=0;
if(tsjw==0||tbs2==0||tbs1==0||brp==0)return 1;
tsjw-=1;
tbs2-=1;
tbs1-=1;
brp-=1;
RCC->APB2ENR|=1<<2;
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE;
GPIOA->CRH&=0X0FFFFFFF;
GPIOA->CRH|=0X80000000;
GPIOA->ODR &=0x7FFF;
GPIOA->CRH&=0XFFF00FFF;
GPIOA->CRH|=0X000B8000;
GPIOA->ODR|=3<<11;
RCC->APB1ENR|=1<<25;
CAN1->MCR=0x0000;
CAN1->MCR|=1<<0;
while((CAN1->MSR&1<<0)==0)
{
i++;
if(i>100)return 2;
}
CAN1->MCR|=0<<7;
CAN1->MCR|=0<<6;
CAN1->MCR|=0<<5;
CAN1->MCR|=1<<4;
CAN1->MCR|=0<<3;
CAN1->MCR|=0<<2;
CAN1->BTR=0x00000000;
CAN1->BTR|=mode<<30;
CAN1->BTR|=tsjw<<24;
CAN1->BTR|=tbs2<<20;
CAN1->BTR|=tbs1<<16;
CAN1->BTR|=brp<<0;
CAN1->MCR&=~(1<<0);
while((CAN1->MSR&1<<0)==1)
{
i++;
if(i>0XFFF0)return 3;
}
CAN1->FMR|=1<<0;
CAN1->FA1R&=~(1<<0);
CAN1->FS1R|=1<<0;
CAN1->FM1R|=0<<0;
CAN1->FFA1R|=0<<0;
CAN1->sFilterRegister[0].FR1=0X00000000;
CAN1->sFilterRegister[0].FR2=0X00000000;
CAN1->FA1R|=1<<0;
CAN1->FMR&=0<<0;
#if CAN_RX0_INT_ENABLE
CAN1->IER|=1<<1;
MY_NVIC_Init(0,0,USB_LP_CAN1_RX0_IRQn,4);
#endif
return 0;
}
u8 CAN_Tx_Msg(u32 id,u8 ide,u8 rtr,u8 len,u8 *dat)
{
u8 mbox;
if(CAN1->TSR&(1<<26))mbox=0;
else if(CAN1->TSR&(1<<27))mbox=1;
else if(CAN1->TSR&(1<<28))mbox=2;
else return 0XFF;
CAN1->sTxMailBox[mbox].TIR=0;
if(ide==0)
{
id&=0x7ff;
id<<=21;
}else
{
id&=0X1FFFFFFF;
id<<=3;
}
CAN1->sTxMailBox[mbox].TIR|=id;
CAN1->sTxMailBox[mbox].TIR|=ide<<2;
CAN1->sTxMailBox[mbox].TIR|=rtr<<1;
len&=0X0F;
CAN1->sTxMailBox[mbox].TDTR&=~(0X0000000F);
CAN1->sTxMailBox[mbox].TDTR|=len;
CAN1->sTxMailBox[mbox].TDHR=(((u32)dat[7]<<24)|
((u32)dat[6]<<16)|
((u32)dat[5]<<8)|
((u32)dat[4]));
CAN1->sTxMailBox[mbox].TDLR=(((u32)dat[3]<<24)|
((u32)dat[2]<<16)|
((u32)dat[1]<<8)|
((u32)dat[0]));
CAN1->sTxMailBox[mbox].TIR|=1<<0;
return mbox;
}
u8 CAN_Tx_Staus(u8 mbox)
{
u8 sta=0;
switch (mbox)
{
case 0:
sta |= CAN1->TSR&(1<<0);
sta |= CAN1->TSR&(1<<1);
sta |=((CAN1->TSR&(1<<26))>>24);
break;
case 1:
sta |= CAN1->TSR&(1<<8)>>8;
sta |= CAN1->TSR&(1<<9)>>8;
sta |=((CAN1->TSR&(1<<27))>>25);
break;
case 2:
sta |= CAN1->TSR&(1<<16)>>16;
sta |= CAN1->TSR&(1<<17)>>16;
sta |=((CAN1->TSR&(1<<28))>>26);
break;
default:
sta=0X05;
break;
}
return sta;
}
u8 CAN_Msg_Pend(u8 fifox)
{
if(fifox==0)return CAN1->RF0R&0x03;
else if(fifox==1)return CAN1->RF1R&0x03;
else return 0;
}
void CAN_Rx_Msg(u8 fifox,u32 *id,u8 *ide,u8 *rtr,u8 *len,u8 *dat)
{
*ide=CAN1->sFIFOMailBox[fifox].RIR&0x04;
if(*ide==0)
{
*id=CAN1->sFIFOMailBox[fifox].RIR>>21;
}else
{
*id=CAN1->sFIFOMailBox[fifox].RIR>>3;
}
*rtr=CAN1->sFIFOMailBox[fifox].RIR&0x02;
*len=CAN1->sFIFOMailBox[fifox].RDTR&0x0F;
dat[0]=CAN1->sFIFOMailBox[fifox].RDLR&0XFF;
dat[1]=(CAN1->sFIFOMailBox[fifox].RDLR>>8)&0XFF;
dat[2]=(CAN1->sFIFOMailBox[fifox].RDLR>>16)&0XFF;
dat[3]=(CAN1->sFIFOMailBox[fifox].RDLR>>24)&0XFF;
dat[4]=CAN1->sFIFOMailBox[fifox].RDHR&0XFF;
dat[5]=(CAN1->sFIFOMailBox[fifox].RDHR>>8)&0XFF;
dat[6]=(CAN1->sFIFOMailBox[fifox].RDHR>>16)&0XFF;
dat[7]=(CAN1->sFIFOMailBox[fifox].RDHR>>24)&0XFF;
if(fifox==0)CAN1->RF0R|=0X20;
else if(fifox==1)CAN1->RF1R|=0X20;
}
#if CAN_RX0_INT_ENABLE
u16 rpm=2990;
int timeUp=0;
int timeDn=0;
u8 TestK=0;
void USB_LP_CAN1_RX0_IRQHandler(void)
{
u8 rxbuf[8];
u32 id;
u8 ide,rtr,len;
int valUp = 0;
char *pBufUp = (char*)&valUp;
int valDn = 0;
char *pBufDn = (char*)&valDn;
CAN_Rx_Msg(0,&id,&ide,&rtr,&len,rxbuf);
pBufUp[0] = rxbuf[5];
pBufUp[1] = rxbuf[4];
pBufDn[0] = rxbuf[5];
pBufDn[1] = rxbuf[4];
CAN_Send_Msg(rxbuf,8);
}
#endif
u8 CAN_Send_Msg(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
mbox=CAN_Tx_Msg(0X12,0,0,len,msg);
while((CAN_Tx_Staus(mbox)!=0X07)&&(i<0XFFF))i++;
if(i>=0XFFF)return 1;
return 0;
}
u8 CAN_Receive_Msg(u8 *buf)
{
u32 id;
u8 ide,rtr,len;
if(CAN_Msg_Pend(0)==0)return 0;
CAN_Rx_Msg(0,&id,&ide,&rtr,&len,buf);
if(id!=0x12||ide!=0||rtr!=0)len=0;
return len;
}
u8 CAN_Tx_Msg_u8(u32 id,u8 ide,u8 rtr,u8 len,u8 *dat)
{
u8 mbox;
if(CAN1->TSR&(1<<26))mbox=0;
else if(CAN1->TSR&(1<<27))mbox=1;
else if(CAN1->TSR&(1<<28))mbox=2;
else return 0XFF;
CAN1->sTxMailBox[mbox].TIR=0;
if(ide==0)
{
id&=0x7ff;
id<<=21;
}else
{
id&=0X1FFFFFFF;
id<<=3;
}
CAN1->sTxMailBox[mbox].TIR|=id;
CAN1->sTxMailBox[mbox].TIR|=ide<<2;
CAN1->sTxMailBox[mbox].TIR|=rtr<<1;
len&=0X0F;
CAN1->sTxMailBox[mbox].TDTR&=~(0X0000000F);
CAN1->sTxMailBox[mbox].TDTR|=len;
CAN1->sTxMailBox[mbox].TDHR=(((u32)dat[7]<<24)|
((u32)dat[6]<<16)|
((u32)dat[5]<<8)|
((u32)dat[4]));
CAN1->sTxMailBox[mbox].TDLR=(((u32)dat[3]<<24)|
((u32)dat[2]<<16)|
((u32)dat[1]<<8)|
((u32)dat[0]));
CAN1->sTxMailBox[mbox].TIR|=1<<0;
return mbox;
}
u8 CAN_Send_Msg_Hex(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
mbox=CAN_Tx_Msg_u8(0X101,0,0,len,msg);
while((CAN_Tx_Staus(mbox)!=0X07)&&(i<0XFFF))i++;
if(i>=0XFFF)return 1;
return 0;
}
u8 CAN_Tx_Msg_u16(u32 id,u8 ide,u8 rtr,u8 len,u16 *dat)
{
u8 mbox;
if(CAN1->TSR&(1<<26))mbox=0;
else if(CAN1->TSR&(1<<27))mbox=1;
else if(CAN1->TSR&(1<<28))mbox=2;
else return 0XFF;
CAN1->sTxMailBox[mbox].TIR=0;
if(ide==0)
{
id&=0x7ff;
id<<=21;
}else
{
id&=0X1FFFFFFF;
id<<=3;
}
CAN1->sTxMailBox[mbox].TIR|=id;
CAN1->sTxMailBox[mbox].TIR|=ide<<2;
CAN1->sTxMailBox[mbox].TIR|=rtr<<1;
len&=0X0F;
CAN1->sTxMailBox[mbox].TDTR&=~(0X0000000F);
CAN1->sTxMailBox[mbox].TDTR|=len;
CAN1->sTxMailBox[mbox].TDHR=(((u32)dat[7]<<24)|
((u32)dat[6]<<16)|
((u32)dat[5]<<8)|
((u32)dat[4]));
CAN1->sTxMailBox[mbox].TDLR=(((u32)dat[3]<<24)|
((u32)dat[2]<<16)|
((u32)dat[1]<<8)|
((u32)dat[0]));
CAN1->sTxMailBox[mbox].TIR|=1<<0;
return mbox;
}
u8 CAN_Send_Msg_Hex_u16(u16* msg,u8 len)
{
u8 mbox;
u16 i=0;
mbox=CAN_Tx_Msg_u16(0X101,0,0,len,msg);
while((CAN_Tx_Staus(mbox)!=0X07)&&(i<0XFFF))i++;
if(i>=0XFFF)return 1;
return 0;
}
|