[DemoCode下载] CRC的单片机应用

[复制链接]
901|8
 楼主| mintspring 发表于 2018-10-27 00:10 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 2 $
  5. * $Date: 15/04/13 10:05a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Implement CRC in CRC-8 mode and get the CRC checksum result.
  7. * @note
  8. * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "NUC100Series.h"

  13. #define PLL_CLOCK           50000000


  14. void SYS_Init(void)
  15. {
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/
  19.     /* Enable Internal RC 22.1184MHz clock */
  20.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  21.     /* Waiting for Internal RC clock ready */
  22.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  23.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  24.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  25.     /* Enable external XTAL 12MHz clock */
  26.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  27.     /* Waiting for clock ready */
  28.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  29.     /* Set core clock as PLL_CLOCK from PLL */
  30.     CLK_SetCoreClock(PLL_CLOCK);
  31.    
  32.     /* Enable UART module clock */
  33.     CLK_EnableModuleClock(UART0_MODULE);   
  34.    
  35.     /* Select UART module clock source */
  36.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
  37.    
  38.     /* Enable PDMA module clock for CRC operation */
  39.     CLK_EnableModuleClock(PDMA_MODULE);   

  40.     /*---------------------------------------------------------------------------------------------------------*/
  41.     /* Init I/O Multi-function                                                                                 */
  42.     /*---------------------------------------------------------------------------------------------------------*/
  43.     /* Set PB multi-function pins for UART0 RXD, TXD */
  44.     SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
  45. }

  46. void UART0_Init(void)
  47. {
  48.     /*---------------------------------------------------------------------------------------------------------*/
  49.     /* Init UART                                                                                               */
  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Reset UART0 */
  52.     SYS_ResetModule(UART0_RST);

  53.     /* Configure UART0 and set UART0 Baudrate */
  54.     UART_Open(UART0, 115200);
  55. }

  56. /*---------------------------------------------------------------------------------------------------------*/
  57. /*  MAIN function                                                                                          */
  58. /*---------------------------------------------------------------------------------------------------------*/
  59. int main(void)
  60. {
  61.     const uint8_t acCRCSrcPattern[] = "123456789";
  62.     uint32_t i, u32TargetChecksum = 0x58, u32CalChecksum = 0;
  63.     uint8_t *p8SrcAddr;

  64.     /* Unlock protected registers */
  65.     SYS_UnlockReg();

  66.     /* Init System, peripheral clock and multi-function I/O */
  67.     SYS_Init();

  68.     /* Lock protected registers */
  69.     SYS_LockReg();

  70.     /* Init UART0 for printf */
  71.     UART0_Init();

  72.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
  73.     printf("+---------------------------------+\n");
  74.     printf("|    CRC CRC-8 Mode Sample Code   |\n");
  75.     printf("+---------------------------------+\n\n");

  76.     printf("# Calculate string "123456789" CRC-8 checksum value by CRC CPU mode.\n");
  77.     printf("    - Seed value is 0x5A            \n");
  78.     printf("    - CPU Write Length is 8-bit     \n");
  79.     printf("    - Checksum Complement disable   \n");
  80.     printf("    - Checksum Reverse disable      \n");
  81.     printf("    - Write Data Complement disable \n");
  82.     printf("    - Write Data Reverse disable    \n");
  83.     printf("    - Checksum should be 0x%X       \n\n", u32TargetChecksum);

  84.     /* Set CRC source buffer address for CRC-8 CPU mode */
  85.     p8SrcAddr = (uint8_t *)acCRCSrcPattern;

  86.     /* Configure CRC operation settings for CRC-8 CPU mode */
  87.     CRC_Open(CRC_8, 0, 0x5A, CRC_CPU_WDATA_8);

  88.     /* Start to execute CRC-8 CPU operation */
  89.     for(i = 0; i < strlen((char *)acCRCSrcPattern); i++)
  90.     {
  91.         CRC_WRITE_DATA((p8SrcAddr[i] & 0xFF));
  92.     }

  93.     /* Get CRC-8 checksum value */
  94.     u32CalChecksum = CRC_GetChecksum();
  95.     printf("CRC checksum is 0x%X ... %s.\n", u32CalChecksum, (u32CalChecksum == u32TargetChecksum) ? "PASS" : "FAIL");

  96.     /* Disable CRC function */
  97.     CRC->CTL &= ~CRC_CTL_CRCCEN_Msk;

  98.     while(1);
  99. }

  100. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


 楼主| mintspring 发表于 2018-10-27 00:11 | 显示全部楼层
这个是单片机上常用的一种。
 楼主| mintspring 发表于 2018-10-27 00:12 | 显示全部楼层
比较简单的。
你会用吗
huangcunxiake 发表于 2018-10-28 11:07 | 显示全部楼层
要求不高的可以不用这个。
hotpower 发表于 2018-12-30 00:01 来自手机 | 显示全部楼层
本帖最后由 hotpower 于 2018-12-30 00:43 编辑

http://www.hotpage.com.cn/hotcrc
xinpian101 发表于 2018-12-30 13:41 | 显示全部楼层
啥情况,头像都带个蛋
yiyigirl2014 发表于 2018-12-30 18:31 | 显示全部楼层
CRC加密用的很多。
hotpower 发表于 2019-1-1 20:23 来自手机 | 显示全部楼层
hotcrc理论上100%**了任意CRC
hotpower 发表于 2019-1-1 20:24 来自手机 | 显示全部楼层
hotcrc理论上100%**了任意CRC
您需要登录后才可以回帖 登录 | 注册

本版积分规则

303

主题

4972

帖子

24

粉丝
快速回复 在线客服 返回列表 返回顶部