Advertisement
Guest User

distance measure

a guest
Jan 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "NUC100Series.h"
  4. #include "MCU_init.h"
  5. #include "SYS_init.h"
  6. #include "LCD.h"
  7. #define SECOND 10000000
  8. #define MILLISECOND 10000
  9. #define MICROSECOND 10
  10.  
  11.  
  12. void System_Config(void);
  13. void Delay_s(uint32_t count);
  14.  
  15. void Timer_Config(void);
  16. void TMR2_Start(void);
  17. void TMR2_Stop(void);
  18.  
  19. void Interrupt_Config(void);
  20. void EINT1_IRQHandler(void);
  21. void trigger(void);
  22.  
  23. void SPI3_Config(void);
  24. void LCD_start(void);
  25. void LCD_command(unsigned char temp);
  26. void LCD_data(unsigned char temp);
  27. void LCD_clear(void);
  28. void LCD_SetAddress(uint8_t PageAddr, uint8_t ColumnAddr);
  29. void GPIO_Config(void);
  30. void HandleDelay(void);
  31.  
  32. uint8_t echo_flag;
  33.  
  34. long blink_delay, dist, trigger_delay, target;
  35.  
  36. int main(void)
  37. {      
  38.     //System initialization------------------------------------------------------
  39.     System_Config();
  40.     Interrupt_Config();
  41.     Timer_Config();
  42.     GPIO_Config();
  43.     SPI3_Config();
  44.     LCD_start();
  45.     LCD_clear();
  46.    
  47.     echo_flag = 0;
  48.    
  49.    
  50.     while (1) {
  51.         HandleDelay();
  52.     }
  53. }
  54.  
  55. void HandleDelay(void) {
  56.     if (trigger_delay > 250) {
  57.         trigger();
  58.         trigger_delay = 0;
  59.     }
  60.     if (dist < 1000)
  61.         target = 20000;
  62.     else if (dist < 2500)
  63.             target = 30000;
  64.     else if (dist < 4000)
  65.         target = 40000;
  66.     else
  67.         target = 50000;
  68.     if (blink_delay > target){
  69.         PC->DOUT ^= (1<<12);
  70.         PB->DOUT ^= (1<<11);
  71.         blink_delay = 0;
  72.     }
  73.     Delay_s(1000);
  74.     trigger_delay++;
  75.     blink_delay += 200;
  76. }
  77.  
  78. //Time Counting and Interrupt Functions-------------------------------------------------------------
  79. void display_dist(void) { //Calculates dist based off timer value, converts to char[] and prints on LCD
  80.     uint8_t i;
  81.     char string[6];
  82.     uint32_t array[5];
  83.     dist *= 17;
  84.     dist /= 11;
  85.     for (i = 0; i < 4; i++)
  86.         array[i] = (dist / (uint32_t)pow(10,4-i)) % 10;
  87.     for (i = 0; i < 4; i++)
  88.         string[i] =  '0' + array[i];   
  89.     print_Line(1, string);
  90.     print_Line(0, "dist(mm):");
  91.     for (i = 0; i < 4; i++)
  92.         string[i] = '0';
  93. }
  94.  
  95. void trigger(void){ //Sends 10 microsecond pulse to sensor TRIG pin
  96.     PB->DOUT ^= 1<<12;
  97.     Delay_s(10);
  98.     PB->DOUT ^= 1<<12;
  99. }
  100.  
  101. void EINT1_IRQHandler(void){ //Interrupt based on sensor ECHO pin rising edge and falling edge
  102.     if (!echo_flag)
  103.         TMR2_Start();
  104.     else {
  105.         TMR2_Stop();
  106.         display_dist();
  107.     }
  108.     echo_flag = !echo_flag;
  109.     PB->ISRC |= (1ul << 15);    // set that interrupt ended
  110. }
  111.  
  112. void TMR2_Start(void){  //Start the timer
  113.     TIMER2->TCSR &= ~(0xFFul << 0);
  114.     TIMER2->TCSR |= (0x9ul << 0); // Now, the timer frequency = 1MHz / (9 + 1) = 100 kHz
  115.     TIMER2->TCMPR = 16777215; //set TCMPR to highest 24-bit value possible (because we don't want the timer to be limited)
  116.     TIMER2->TCSR |= (1ul << 30); //Activate TMR2
  117. }
  118.  
  119. void TMR2_Stop(void){ //Stop the timer
  120.     TIMER2->TCSR &= ~(1ul << 30); //Deactivate TMR2
  121.     dist = TIMER2->TDR;
  122.     TIMER2->TCSR |= (1ul << 26); //Reset TMR2 prescale, count value, and bit 30
  123. }
  124.    
  125. void Delay_s(uint32_t count){ //Basic delay function
  126.     uint32_t n;
  127.     for(n=0;n<count;n++);
  128. }
  129.  
  130.  
  131. // System and Module Configuration Functions---------------------------------------
  132. void System_Config(void){
  133.     SYS_UnlockReg(); // Unlock protected registers
  134.  
  135.     //Enable clock source: HXT 12MHz
  136.     CLK->PWRCON |= (1ul << 0);
  137.     while(!(CLK->CLKSTATUS & (1ul << 0)));
  138.        
  139.     //PLL configuration starts
  140.     CLK->PLLCON &= ~(1ul<<19); //0: PLL input is HXT
  141.     CLK->PLLCON &= ~(1ul<<16); //PLL in normal mode
  142.     CLK->PLLCON &= (~(0x01FFul << 0));
  143.     CLK->PLLCON |= 10; //Frequency is now = 12 / (10 + 2) = 1 MHz
  144.     CLK->PLLCON &= ~(1ul<<18); //0: enable PLLOUT
  145.     while(!(CLK->CLKSTATUS & (0x01ul << 2)));
  146.     //PLL configuration ends
  147.  
  148.     //clock source selection
  149.     CLK->CLKSEL0 &= (~(0x07ul << 0));
  150.     CLK->CLKSEL0 |= (0x02ul << 0);
  151.     //clock frequency division
  152.     CLK->CLKDIV &= (~0x0Ful << 0);
  153.     //enable clock of SPI3
  154.     CLK->APBCLK |= 1ul << 15;
  155.  
  156.     SYS_LockReg(); // Lock protected registers
  157. }
  158.  
  159. void SPI3_Config (void){
  160.     SYS->GPD_MFP |= 1ul << 11; //1: PD11 is configured for alternative function
  161.     SYS->GPD_MFP |= 1ul << 9; //1: PD9 is configured for alternative function
  162.     SYS->GPD_MFP |= 1ul << 8; //1: PD8 is configured for alternative function
  163.  
  164.     SPI3->CNTRL &= ~(1ul << 23); //0: disable variable clock feature
  165.  
  166.  
  167.     SPI3->CNTRL &= ~(1ul << 22); //0: disable two bits transfer mode
  168.     SPI3->CNTRL &= ~(1ul << 18); //0: select Master mode
  169.     SPI3->CNTRL &= ~(1ul << 17); //0: disable SPI interrupt
  170.     SPI3->CNTRL |= 1ul << 11; //1: SPI clock idle high
  171.     SPI3->CNTRL &= ~(1ul << 10); //0: MSB is sent echo_flag
  172.     SPI3->CNTRL &= ~(3ul << 8); //00: one transmit/receive word will be executed in one data transfer
  173.  
  174.     SPI3->CNTRL &= ~(31ul << 3); //Transmit/Receive bit length
  175.     SPI3->CNTRL |= 9ul << 3; //9: 9 bits transmitted/received per data transfer
  176.  
  177.     SPI3->CNTRL |= (1ul << 2); //1: Transmit at negative edge of SPI CLK
  178.     SPI3->DIVIDER = 0; // SPI clock divider. SPI clock = HCLK / ((DIVIDER+1)*2). HCLK = 50 MHz
  179. }
  180.  
  181. void Interrupt_Config(void) {
  182.     //hook echo pin = GPIO-B15
  183.    
  184.     //GPIO Interrupt configuration.  is the interrupt source
  185.     PB->PMD &= (~(0x03ul << 30));       // Input mode
  186.     PB->IMD &= (~(1ul << 15));          // 0: edge level detection
  187.     PB->IEN |= (1ul << 15);                 // enable falling edge trigger
  188.     PB->IEN |= (1ul << (15+16));        // enable rising edge trigger as well
  189.  
  190.     //NVIC interrupt configuration for GPIO-B15 interrupt source
  191.     NVIC->ISER[0] |= 1ul<<3;
  192.     NVIC->IP[0] &= (~(3ul<<30));
  193. }
  194.  
  195. void Timer_Config(void){
  196.     CLK->CLKSEL1 &= ~(0x07ul << 16);
  197.     CLK->CLKSEL1 |= (0x02ul << 16); //Use HCLK as TMR2 source
  198.     CLK->APBCLK |= (0x01ul << 4);       //Enable TMR2 channel
  199.     TIMER2->TCSR &= ~(0xFFul << 0);
  200.     TIMER2->TCSR |= (0x9ul << 0); // Now, the timer frequency = 1MHz / (9 + 1) = 100 kHz
  201.     TIMER2->TCSR &= ~(0x03ul << 27); //set TMR2 to One-shot mode
  202.     TIMER2->TCMPR = 16777215; //set TCMPR to highest 24-bit value possible (we want to avoid timer limitations)
  203.     TIMER2->TCSR |= (0x01ul << 16); //TDR enabled
  204.     TIMER2->TCSR &= ~(0x01ul << 29); //Disable TMR2 interrupt
  205.     TIMER2->TISR |= (0x01ul << 0); //Clear interrupt flag
  206.     TIMER2->TCSR &= ~(1ul << 30); //Deactivate TMR2
  207.     }
  208.  
  209. void GPIO_Config(void) {
  210.     //Configuring buzzer and LED
  211.     PC->PMD &= (~(0x03ul << 24));
  212.     PC->PMD |= (0x01ul << 24); 
  213.    
  214.     PB->PMD &= (~(0x03ul << 22));
  215.     PB->PMD |= (0x01ul << 22); 
  216.    
  217.     //Configure PB15 to send 10 microsecond pulse to trigger pin
  218.     PB->PMD &= (~(0x03ul << 24));
  219.     PB->PMD |= (0x01ul << 24);
  220.    
  221. }
  222.  
  223.  
  224. // LCD Functions---------------------------------------------------------------------
  225. void LCD_start(void) {
  226.     LCD_command(0xE2); // Set system reset
  227.     LCD_command(0xA1); // Set Frame rate 100 fps
  228.     LCD_command(0xEB); // Set LCD bias ratio E8~EB for 6~9 (min~max)
  229.     LCD_command(0x81); // Set V BIAS potentiometer
  230.     LCD_command(0xA0); // Set V BIAS potentiometer: A0 ()
  231.     LCD_command(0xC0);
  232.     LCD_command(0xAF); // Set Display Enable
  233. }
  234.  
  235. void LCD_command(unsigned char temp) {
  236.     SPI3->SSR |= 1ul << 0;
  237.     SPI3->TX[0] = temp;
  238.     SPI3->CNTRL |= 1ul << 0;
  239.     while(SPI3->CNTRL & (1ul << 0));
  240.     SPI3->SSR &= ~(1ul << 0);
  241. }
  242.  
  243. void LCD_data(unsigned char temp) {
  244.     SPI3->SSR |= 1ul << 0;
  245.     SPI3->TX[0] = 0x0100+temp;
  246.     SPI3->CNTRL |= 1ul << 0;
  247.     while(SPI3->CNTRL & (1ul << 0));
  248.     SPI3->SSR &= ~(1ul << 0);
  249. }
  250.  
  251. void LCD_clear(void) {
  252.     int16_t i;
  253.     LCD_SetAddress(0x0, 0x0);
  254.     for (i = 0; i < 132 *8; i++)
  255.         LCD_data(0x00);
  256.    
  257. }
  258.  
  259. void LCD_SetAddress(uint8_t PageAddr, uint8_t ColumnAddr) {
  260.     LCD_command(0xB0 | PageAddr);
  261.     LCD_command(0x10 | (ColumnAddr>>4)&0xF);
  262.     LCD_command(0x00 | (ColumnAddr & 0xF));
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement