這個燈具控制程序是采用了單片STC的芯片。
非常好用。
單片機源程序如下:
- #include "stm32f10x.h"
- #include "GLCD.h"
- #include "UART.h"
- typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
- vu32 ret; /* for return of the interrupt handling */
- volatile TestStatus TestRx;
- ErrorStatus HSEStartUpStatus;
- TestStatus CAN_Polling(void);
- TestStatus CAN_Interrupt(void);
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Configure PD.02, PD.03, PD.04 and PD.07 as Output push-pull */
- // For STM3210B-LK1 use PD.02 -PC.07
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- /* Configure CAN pin: RX */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- /* Configure CAN pin: TX */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE ); //重影射CAN IO腳到 PB8,PB9
- }
- //系統中斷管理
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- #ifdef VECT_TAB_RAM
- /* Set the Vector Table base location at 0x20000000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else /* VECT_TAB_FLASH */
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
- /* enabling interrupt */
- NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- //配置系統時鐘,使能各外設時鐘
- void RCC_Configuration(void)
- {
- SystemInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
- |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
- |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
- |RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO
- |RCC_APB2Periph_SPI1, ENABLE );
- //RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL ,ENABLE );
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4
- |RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2
- , ENABLE );
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- /* CAN Periph clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
- }
- void InitDis(void)
- {
- /* LCD Module init */
- GLCD_init();
- GLCD_clear(White);
- GLCD_setTextColor(Blue);
- GLCD_displayStringLn(Line1, " FireBull");
- GLCD_displayStringLn(Line2, " CAN example");
- GLCD_setTextColor(Red);
- }
- //配置所有外設
- void Init_All_Periph(void)
- {
- RCC_Configuration();
- InitDis();
- // GLCD_Test();
- GPIO_Configuration();
- NVIC_Configuration();
- }
- int main(void)
- {
- Init_All_Periph();
- UART1Init();
- GPIO_SetBits(GPIOB, GPIO_Pin_14);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- GPIO_SetBits(GPIOB, GPIO_Pin_13);
- /* CAN transmit at 100Kb/s and receive by polling in loopback mode*/
- // TestRx = CAN_Polling();
- // if (TestRx == FAILED)
- // {
- /* Turn on led connected to PD.04 pin (LD3) */
- // GPIO_SetBits(GPIOB, GPIO_Pin_14);
- // }
- // else
- // {
- /* Turn on led connected to PD.02 pin (LD1) */
- // GPIO_SetBits(GPIOB, GPIO_Pin_12);
- // }
- /* CAN transmit at 500Kb/s and receive by interrupt in loopback mode*/
- TestRx = CAN_Interrupt();
- // if (TestRx == FAILED)
- // {
- /* Turn on led connected to PD.07 pin (LD4) */
- // GPIO_SetBits(GPIOB, GPIO_Pin_13);
- // }
- // else
- // {
- /* Turn on led connected to PD.03 pin (LD2) */
- // GPIO_SetBits(GPIOB, GPIO_Pin_12);
- // }
- while(1)
- {
- }
- }
- /*******************************************************************************
- * Function Name : CAN_Polling
- * Description : Configures the CAN and transmit and receive by polling
- * Input : None
- * Output : None
- * Return : PASSED if the reception is well done, FAILED in other case
- *******************************************************************************/
- TestStatus CAN_Polling(void)
- {
- CAN_InitTypeDef CAN_InitStructure;
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
- CanTxMsg TxMessage;
- CanRxMsg RxMessage;
- u32 i = 0;
- u8 TransmitMailbox;
- /* CAN register init */
- CAN_DeInit(CAN1);
- CAN_StructInit(&CAN_InitStructure);
- /* CAN cell init */
- CAN_InitStructure.CAN_TTCM=DISABLE;
- CAN_InitStructure.CAN_ABOM=DISABLE;
- CAN_InitStructure.CAN_AWUM=DISABLE;
- CAN_InitStructure.CAN_NART=DISABLE;
- CAN_InitStructure.CAN_RFLM=DISABLE;
- CAN_InitStructure.CAN_TXFP=DISABLE;
- CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;//CAN_Mode_LoopBack;
- CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
- CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
- CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
- CAN_InitStructure.CAN_Prescaler=5;
- CAN_Init(CAN1,&CAN_InitStructure);
- /* CAN filter init */
- CAN_FilterInitStructure.CAN_FilterNumber=0;
- CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
- CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
- CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
- CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
- CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;
- CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
- CAN_FilterInit(&CAN_FilterInitStructure);
- /* transmit */
- TxMessage.StdId=0x11;
- TxMessage.RTR=CAN_RTR_DATA;
- TxMessage.IDE=CAN_ID_STD;
- TxMessage.DLC=2;
- TxMessage.Data[0]=0xCA;
- TxMessage.Data[1]=0xFE;
- TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
- i = 0;
- while((CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK) && (i != 0xFF))
- {
- i++;
- }
- i = 0;
- while((CAN_MessagePending(CAN1,CAN_FIFO0) < 1) && (i != 0xFF))
- {
- i++;
- }
- /* receive */
- RxMessage.StdId=0x00;
- RxMessage.IDE=CAN_ID_STD;
- RxMessage.DLC=0;
- RxMessage.Data[0]=0x00;
- RxMessage.Data[1]=0x00;
- CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
- if (RxMessage.StdId!=0x11) return FAILED;
- if (RxMessage.IDE!=CAN_ID_STD) return FAILED;
- if (RxMessage.DLC!=2) return FAILED;
- if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE) return FAILED;
-
- return PASSED; /* Test Passed */
- }
- /*******************************************************************************
- * Function Name : CAN_Interrupt
- * Description : Configures the CAN and transmit and receive by interruption
- * Input : None
- * Output : None
- * Return : PASSED if the reception is well done, FAILED in other case
- *******************************************************************************/
- TestStatus CAN_Interrupt(void)
- {
- CAN_InitTypeDef CAN_InitStructure;
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
- CanTxMsg TxMessage;
- u32 i = 0;
- /* CAN register init */
- CAN_DeInit(CAN1);
- CAN_StructInit(&CAN_InitStructure);
- /* CAN cell init */
- CAN_InitStructure.CAN_TTCM=DISABLE;
- CAN_InitStructure.CAN_ABOM=DISABLE;
- CAN_InitStructure.CAN_AWUM=DISABLE;
- CAN_InitStructure.CAN_NART=DISABLE;
- CAN_InitStructure.CAN_RFLM=DISABLE;
- CAN_InitStructure.CAN_TXFP=DISABLE;
- CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;//CAN_Mode_LoopBack;
- CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
- CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
- CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
- CAN_InitStructure.CAN_Prescaler=1;
- CAN_Init(CAN1,&CAN_InitStructure);
- /* CAN filter init */
- CAN_FilterInitStructure.CAN_FilterNumber=1;
- CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
- CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
- CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
- CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
- CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
- CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
- CAN_FilterInit(&CAN_FilterInitStructure);
- /* CAN FIFO0 message pending interrupt enable */
- CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
- /* transmit 1 message */
- /* TxMessage.StdId=0x00;
- TxMessage.ExtId=0x1234;
- TxMessage.IDE=CAN_ID_EXT;
- TxMessage.RTR=CAN_RTR_DATA;
- TxMessage.DLC=2;
- TxMessage.Data[0]=0xDE;
- TxMessage.Data[1]=0xCA;
- CAN_Transmit(CAN1,&TxMessage);
- /* initialize the value that will be returned */
- /* ret = 0xFF;
-
- /* receive message with interrupt handling */
- /* i=0;
- while((ret == 0xFF) && (i < 0xFFF))
- {
- i++;
- }
-
- if (i == 0xFFF)
- {
- ret=0;
- }
- /* disable interrupt handling */
- // CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);
- return (TestStatus)ret;
- }
- /*******************************************************************************
- * Function Name : USB_LP_CAN_RX0_IRQHandler
- * Description : This function handles USB Low Priority or CAN RX0 interrupts
- * requests.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void USB_LP_CAN1_RX0_IRQHandler(void)
- {
- CanRxMsg RxMessage;
- RxMessage.StdId=0x00;
- RxMessage.ExtId=0x00;
- RxMessage.IDE=0;
- RxMessage.DLC=0;
- RxMessage.FMI=0;
- RxMessage.Data[0]=0x00;
- RxMessage.Data[1]=0x00;
- CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
- UART1SendByte(RxMessage.Data[0]);
- UART1SendByte(RxMessage.Data[1]);
- //GPIO_ResetBits(GPIOB, GPIO_Pin_13);
- if((RxMessage.Data[0]==0x99)&&(RxMessage.Data[1]==0xbb))
- { GPIO_ResetBits(GPIOB, GPIO_Pin_14);}
- if((RxMessage.Data[0]==0x55)&&(RxMessage.Data[1]==0x77))
- { GPIO_ResetBits(GPIOB, GPIO_Pin_12);}
- if((RxMessage.Data[0]==0x11)&&(RxMessage.Data[1]==0x33))
- { GPIO_ResetBits(GPIOB, GPIO_Pin_13); }
-
- /* if((RxMessage.ExtId==0x1234) && (RxMessage.IDE==CAN_ID_EXT)
- && (RxMessage.DLC==2) && ((RxMessage.Data[1]|RxMessage.Data[0]<<8)==0xDECA))
- {
- ret = 1;
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
-
- }*/
-
- }
復制代碼
所有資料51hei提供下載:
發送.zip
(345.75 KB, 下載次數: 4)
2019-8-30 13:47 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
接收.zip
(371.52 KB, 下載次數: 5)
2019-8-30 13:47 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|