Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include "NUC100Series.h"
- #include "MCU_init.h"
- #include "SYS_init.h"
- #include "LCD.h"
- #define SECOND 10000000
- #define MILLISECOND 10000
- #define MICROSECOND 10
- void System_Config(void);
- void Delay_s(uint32_t count);
- void Timer_Config(void);
- void TMR2_Start(void);
- void TMR2_Stop(void);
- void Interrupt_Config(void);
- void EINT1_IRQHandler(void);
- void trigger(void);
- void SPI3_Config(void);
- void LCD_start(void);
- void LCD_command(unsigned char temp);
- void LCD_data(unsigned char temp);
- void LCD_clear(void);
- void LCD_SetAddress(uint8_t PageAddr, uint8_t ColumnAddr);
- void GPIO_Config(void);
- void HandleDelay(void);
- uint8_t echo_flag;
- long blink_delay, dist, trigger_delay, target;
- int main(void)
- {
- //System initialization------------------------------------------------------
- System_Config();
- Interrupt_Config();
- Timer_Config();
- GPIO_Config();
- SPI3_Config();
- LCD_start();
- LCD_clear();
- echo_flag = 0;
- while (1) {
- HandleDelay();
- }
- }
- void HandleDelay(void) {
- if (trigger_delay > 250) {
- trigger();
- trigger_delay = 0;
- }
- if (dist < 1000)
- target = 20000;
- else if (dist < 2500)
- target = 30000;
- else if (dist < 4000)
- target = 40000;
- else
- target = 50000;
- if (blink_delay > target){
- PC->DOUT ^= (1<<12);
- PB->DOUT ^= (1<<11);
- blink_delay = 0;
- }
- Delay_s(1000);
- trigger_delay++;
- blink_delay += 200;
- }
- //Time Counting and Interrupt Functions-------------------------------------------------------------
- void display_dist(void) { //Calculates dist based off timer value, converts to char[] and prints on LCD
- uint8_t i;
- char string[6];
- uint32_t array[5];
- dist *= 17;
- dist /= 11;
- for (i = 0; i < 4; i++)
- array[i] = (dist / (uint32_t)pow(10,4-i)) % 10;
- for (i = 0; i < 4; i++)
- string[i] = '0' + array[i];
- print_Line(1, string);
- print_Line(0, "dist(mm):");
- for (i = 0; i < 4; i++)
- string[i] = '0';
- }
- void trigger(void){ //Sends 10 microsecond pulse to sensor TRIG pin
- PB->DOUT ^= 1<<12;
- Delay_s(10);
- PB->DOUT ^= 1<<12;
- }
- void EINT1_IRQHandler(void){ //Interrupt based on sensor ECHO pin rising edge and falling edge
- if (!echo_flag)
- TMR2_Start();
- else {
- TMR2_Stop();
- display_dist();
- }
- echo_flag = !echo_flag;
- PB->ISRC |= (1ul << 15); // set that interrupt ended
- }
- void TMR2_Start(void){ //Start the timer
- TIMER2->TCSR &= ~(0xFFul << 0);
- TIMER2->TCSR |= (0x9ul << 0); // Now, the timer frequency = 1MHz / (9 + 1) = 100 kHz
- TIMER2->TCMPR = 16777215; //set TCMPR to highest 24-bit value possible (because we don't want the timer to be limited)
- TIMER2->TCSR |= (1ul << 30); //Activate TMR2
- }
- void TMR2_Stop(void){ //Stop the timer
- TIMER2->TCSR &= ~(1ul << 30); //Deactivate TMR2
- dist = TIMER2->TDR;
- TIMER2->TCSR |= (1ul << 26); //Reset TMR2 prescale, count value, and bit 30
- }
- void Delay_s(uint32_t count){ //Basic delay function
- uint32_t n;
- for(n=0;n<count;n++);
- }
- // System and Module Configuration Functions---------------------------------------
- void System_Config(void){
- SYS_UnlockReg(); // Unlock protected registers
- //Enable clock source: HXT 12MHz
- CLK->PWRCON |= (1ul << 0);
- while(!(CLK->CLKSTATUS & (1ul << 0)));
- //PLL configuration starts
- CLK->PLLCON &= ~(1ul<<19); //0: PLL input is HXT
- CLK->PLLCON &= ~(1ul<<16); //PLL in normal mode
- CLK->PLLCON &= (~(0x01FFul << 0));
- CLK->PLLCON |= 10; //Frequency is now = 12 / (10 + 2) = 1 MHz
- CLK->PLLCON &= ~(1ul<<18); //0: enable PLLOUT
- while(!(CLK->CLKSTATUS & (0x01ul << 2)));
- //PLL configuration ends
- //clock source selection
- CLK->CLKSEL0 &= (~(0x07ul << 0));
- CLK->CLKSEL0 |= (0x02ul << 0);
- //clock frequency division
- CLK->CLKDIV &= (~0x0Ful << 0);
- //enable clock of SPI3
- CLK->APBCLK |= 1ul << 15;
- SYS_LockReg(); // Lock protected registers
- }
- void SPI3_Config (void){
- SYS->GPD_MFP |= 1ul << 11; //1: PD11 is configured for alternative function
- SYS->GPD_MFP |= 1ul << 9; //1: PD9 is configured for alternative function
- SYS->GPD_MFP |= 1ul << 8; //1: PD8 is configured for alternative function
- SPI3->CNTRL &= ~(1ul << 23); //0: disable variable clock feature
- SPI3->CNTRL &= ~(1ul << 22); //0: disable two bits transfer mode
- SPI3->CNTRL &= ~(1ul << 18); //0: select Master mode
- SPI3->CNTRL &= ~(1ul << 17); //0: disable SPI interrupt
- SPI3->CNTRL |= 1ul << 11; //1: SPI clock idle high
- SPI3->CNTRL &= ~(1ul << 10); //0: MSB is sent echo_flag
- SPI3->CNTRL &= ~(3ul << 8); //00: one transmit/receive word will be executed in one data transfer
- SPI3->CNTRL &= ~(31ul << 3); //Transmit/Receive bit length
- SPI3->CNTRL |= 9ul << 3; //9: 9 bits transmitted/received per data transfer
- SPI3->CNTRL |= (1ul << 2); //1: Transmit at negative edge of SPI CLK
- SPI3->DIVIDER = 0; // SPI clock divider. SPI clock = HCLK / ((DIVIDER+1)*2). HCLK = 50 MHz
- }
- void Interrupt_Config(void) {
- //hook echo pin = GPIO-B15
- //GPIO Interrupt configuration. is the interrupt source
- PB->PMD &= (~(0x03ul << 30)); // Input mode
- PB->IMD &= (~(1ul << 15)); // 0: edge level detection
- PB->IEN |= (1ul << 15); // enable falling edge trigger
- PB->IEN |= (1ul << (15+16)); // enable rising edge trigger as well
- //NVIC interrupt configuration for GPIO-B15 interrupt source
- NVIC->ISER[0] |= 1ul<<3;
- NVIC->IP[0] &= (~(3ul<<30));
- }
- void Timer_Config(void){
- CLK->CLKSEL1 &= ~(0x07ul << 16);
- CLK->CLKSEL1 |= (0x02ul << 16); //Use HCLK as TMR2 source
- CLK->APBCLK |= (0x01ul << 4); //Enable TMR2 channel
- TIMER2->TCSR &= ~(0xFFul << 0);
- TIMER2->TCSR |= (0x9ul << 0); // Now, the timer frequency = 1MHz / (9 + 1) = 100 kHz
- TIMER2->TCSR &= ~(0x03ul << 27); //set TMR2 to One-shot mode
- TIMER2->TCMPR = 16777215; //set TCMPR to highest 24-bit value possible (we want to avoid timer limitations)
- TIMER2->TCSR |= (0x01ul << 16); //TDR enabled
- TIMER2->TCSR &= ~(0x01ul << 29); //Disable TMR2 interrupt
- TIMER2->TISR |= (0x01ul << 0); //Clear interrupt flag
- TIMER2->TCSR &= ~(1ul << 30); //Deactivate TMR2
- }
- void GPIO_Config(void) {
- //Configuring buzzer and LED
- PC->PMD &= (~(0x03ul << 24));
- PC->PMD |= (0x01ul << 24);
- PB->PMD &= (~(0x03ul << 22));
- PB->PMD |= (0x01ul << 22);
- //Configure PB15 to send 10 microsecond pulse to trigger pin
- PB->PMD &= (~(0x03ul << 24));
- PB->PMD |= (0x01ul << 24);
- }
- // LCD Functions---------------------------------------------------------------------
- void LCD_start(void) {
- LCD_command(0xE2); // Set system reset
- LCD_command(0xA1); // Set Frame rate 100 fps
- LCD_command(0xEB); // Set LCD bias ratio E8~EB for 6~9 (min~max)
- LCD_command(0x81); // Set V BIAS potentiometer
- LCD_command(0xA0); // Set V BIAS potentiometer: A0 ()
- LCD_command(0xC0);
- LCD_command(0xAF); // Set Display Enable
- }
- void LCD_command(unsigned char temp) {
- SPI3->SSR |= 1ul << 0;
- SPI3->TX[0] = temp;
- SPI3->CNTRL |= 1ul << 0;
- while(SPI3->CNTRL & (1ul << 0));
- SPI3->SSR &= ~(1ul << 0);
- }
- void LCD_data(unsigned char temp) {
- SPI3->SSR |= 1ul << 0;
- SPI3->TX[0] = 0x0100+temp;
- SPI3->CNTRL |= 1ul << 0;
- while(SPI3->CNTRL & (1ul << 0));
- SPI3->SSR &= ~(1ul << 0);
- }
- void LCD_clear(void) {
- int16_t i;
- LCD_SetAddress(0x0, 0x0);
- for (i = 0; i < 132 *8; i++)
- LCD_data(0x00);
- }
- void LCD_SetAddress(uint8_t PageAddr, uint8_t ColumnAddr) {
- LCD_command(0xB0 | PageAddr);
- LCD_command(0x10 | (ColumnAddr>>4)&0xF);
- LCD_command(0x00 | (ColumnAddr & 0xF));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement