Contex-M0 (lpc1114)-based tinyhmi keyboard driver

Source: Internet
Author: User

 

/*************************************** **************************************** * ************************ <Br/> macro definition <br/> ***** **************************************** **************************************** * ******************/<br/> # define rck (1ul <2) <br/> # define rck_low gpio2data & = ~ Rck <br/> # define rck_high gpio2data | = rck <br/> # define rck_set iocon_pio2_2 = func_pio2_2; gpio2dir | = rck <br/> # define key (1ul <9) <br/> # define key_set iocon_pio2_9 = func_pio2_9; gpio2dir & = ~ Key <br/> # define mosi_set iocon_pio0_9 = pio0_9_mosi0 <br/> # define clk_set iocon_pio0_6 = empty <br/> /************** **************************************** **************************************** * ********** <br/> global variables <br/> ******************* **************************************** **************************************** * *****/<br/> uint32 const data [11] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0 X07, 0x7f, 0x6f, 0x00}; // segment code table, 0 ~ 9 and "Black Code" <br/> uint8 bit [8] = {0, 0, 0, 0, 0, 0}; // keep each bit after decomposition (displayed on the digital tube) <br/> int I _move_bit = 0; // bitcode cyclic variable <br/> uint8 whichkeydown = 9; // indicates which key is pressed, 0 ~ Key 7. <Br/> uint8 modifybit = 9; // bit being modified <br/> uint8 modify = 0; // whether the State is being modified, 1 is yes, 0: No <br/> uint32 num_right4bit = 3210, num_left4bit = 7654; // X is displayed on the right, and Y is on the left <br/> uint32 blackbitdly = 0; // latency variable in the blinking bit <br/> /*************************** **************************************** * ******** <br/> ** breaknum_right function: break down a 4-digit digital tube to the right <br/> *************************** **************************************** * ********/<br/> void break Num_right (INT const num) <br/>{< br/> bit [0] = num % 10; <br/> bit [1] = (Num/10) % 10; <br/> bit [2] = (Num/100) % 10; <br/> bit [3] = (Num/1000) % 10; <br/>}< br/> /****************************** **************************************** * ***** <br/> ** breaknum_left function: break down a four-digit digital tube to the left <br/> *************************** **************************************** * ********/<br/> void breaknum_left (INT const num) <br/>{< br/> B It [4] = num % 10; <br/> bit [5] = (Num/10) % 10; <br/> bit [6] = (Num/100) % 10; <br/> bit [6] = (Num/1000) % 10; <br/>}< br/> /****************************** **************************************** * ***** <br/> ** keyboardinit function: parameters required to initialize the keyboard Board <br/> **************************** **************************************** * *******/<br/> void keyboardinit (void) <br/> {<br/> // num_right4bit = 3210; num_left4bit = 7654; <br/> brea Knum_right (num_right4bit); <br/> breaknum_left (num_left4bit ); <br/>}< br/> /****************************** **************************************** * ***** <br/> ** sendnum function: send a scan <br/> ********************************* **************************************** * **/<br/> void sendnum (void) <br/>{< br/> rck_low; // STR low <br/> If (I _move_bit = modifybit) // in the modified state, flashing change bit <br/>{< br/> blackbitdly ++; <br/> If (BL Ackbitdly <30) <br/>{< br/> ssp0dr = data [10]; // send a black code disconnection Code <br/>}< br/> else <br/> ssp0dr = data [bit [I _move_bit]; // transfer data a content (normal segment code) <br/> If (blackbitdly> 60) <br/> blackbitdly = 0; <br/>}< br/> else // in non-modified state, no blinking <br/>{< br/> ssp0dr = data [bit [I _move_bit]; // The transmitted data a content (normal field code) <br/>}< br/> while (ssp0sr & 0x01) = 0); // wait until the transfer ends <br/> ssp0dr = 0xff &(~ (1 <(I _move_bit); // transmitted data B content (bit code) <br/> while (ssp0sr & 0x01) = 0 ); // wait until the transfer ends <br/> rck_high; // STR high level (generates a rising edge) <br/>}< br/> /****************************** **************************************** * ***** <br/> ** gpio_init <br/> *********************** **************************************** * ************/<br/> void gpio_init (void) <br/>{< br/> rck_set; <br/> clk_set; <br/> mosi_set; <br/> key_set; </P> <p> I Ocon_pio0_2 = pio0_2_gpio; <br/>}< br/> /****************************** **************************************** * ********************************* <br/> * * Function Name: ssp_init () <br/> ** descriptions: Set SSP controller to host SPI. <Br/> ** input parameters: None <br/> ** output parameters: None <br/> ** returned value: none <br/> *********************************** **************************************** * ****************************/<br/> void ssp_init (void) <br/> {<br/> presetctrl | = 0x01;/* disable spi0 reset */<br/> // iocon_sckloc = 0x02; /* configure IIS6 as sck */<br/> sysahbclkctrl | = (1ul <11);/* enable spi0 peripherals */<br/> ssp0clkdiv = 0x01; /* SSP clock points Frequency */</P> <p> ssp0cr0 = (0x00 <8) | // This sentence is the culprit of my tragedy for a week !!! (0x01 <8) frequency division causes frequency problems !!! /* Set SPI clock frequency division for SCR */<br/> (0x00 <7) |/* cpha clock output phase, */<br/>/* only valid in SPI mode */<br/> (0x01 <6) |/* cpol clock output polarity, */<br/>/* only valid in SPI mode */<br/> (0x00 <4) |/* FRF frame format 00 = SPI, 01 = SSI, */<br/>/* 10 = Microwire, 11 = reserved */<br/> (0x07 <0);/* DSS Data Length, 0000-0010 = reserved */<br/>/* 0011 = 4 bits, 0111 = 8 bits, 1111 = 16-bit */<br/> ssp0cr1 = (0x00 <3) |/* output of SOD from the server, 1 = disabled */<br/> (0x00 <2) |/* MS Master/Slave selection, 0 = host, 1 = slave */<B R/> (0x01 <1) |/* sse ssp enabling */<br/> (0x00 <0 ); /* LBM write-back mode */</P> <p> ssp0cpsr = 2;/* pclk crossover value */<br/> ssp0rn = 0x03; /* interrupt clearing register */<br/>}< br/> /*********************** **************************************** * ************ <br/> ** interrupt service program timer0_server <br/> ************* **************************************** * *********************/<br/> void timer0isr (void) <br/>{< br/> sendnum (); <br/> If (0 = (gpio2data & Key) // If a key is pressed, <br/> {// obtain the key ID by connecting to the Key location code <br/> whichkeydown = I _move_bit; <br/>}< br/> else if (I _move_bit = whichkeydown) // process keys <br/>{< br/> switch (I _move_bit) <br/> {<br/> case 7: {// enter or exit the modification state <br/> modify =! Modify; <br/> if (1 = Modify) // If the modified state is entered <br/> {<br/> modifybit = 0; <br/>}< br/> else // If the status is not modified <br/> {<br/> num_right4bit = bit [3] * 1000 + bit [2] * 100 + bit [1] * 10 + bit [0]; <br/> num_left4bit = bit [7] * 1000 + bit [6] * 100 + bit [5] * 10 + bit [4]; <br/> breaknum_right (num_right4bit); <br/> breaknum_left (num_left4bit); <br/> modifybit = 9; <br/>}< br/>} break; <br/> case 6: {// move the modified digit <br/> if (1 = Modify) // if you enter the modified state <br/>{< br/> modifybit = (modifybit + 1) % 8; <br/>}< br/>} break; <br/> case 5: {// modify digits + 1 <br/> if (1 = Modify) // if you enter the modified state <br/>{< br/> bit [modifybit] = (bit [modifybit] + 1) % 10; // function: + 1 </P> <p >}< br/>} break; <br/> case 4: {// modify the digit-1 <br/> if (1 = Modify) // if you enter the modified state <br/>{< br/> bit [modifybit] = (bit [modifybit] + 9) % 10; // function:-1 <br/>}< br/>} break; <br/> case 3: {</P> <p >} break; <br/> case 2 :{</P> <p>} break; <br/> case 1 :{</P> <p>} break; <br/> case 0: {</P> <p>} break; <br/> default: {<br/> break; <br/>}< br/> whichkeydown = 9; <br/>}< br/> I _move_bit = (I _move_bit + 1) & 0x07; // bitcode loop // This write improves efficiency <br/> tmr32b0ir = 0x01; // clear the interrupt mark <br/>}< br/> /************************* **************************************** * ********** <br/> ** timer0init <br/> ****************** **************************************** * ****************/<br/> void timer0init (void) <br/> {<br/> sysahbclkctrl | = (1ul <9); // enable the timer module <br/> tmr32b0ctl = 0x02; // reset the timer (reset) <br/> tmr32b0pr = 0x00; // No Frequency Division <br/> tmr32b0mcr = 0x03; // interrupt occurs during Matching and reset the counter <br/> tmr32b0mr0 = fahbclk/500; // match once every 1/500 seconds (although the scanning frequency of timer interruption is 500, however, the average frequency of each digital digit is only 500/8. (If the actual frequency is greater than 300, no flash will be visible.) <br/> tmr32b0ir | = 0x01; // clear the channel 0 (mr0) interrupt flag <br/> zyisrset (nvic_timer32b0, (unsigned long) timer0isr, prio_two ); // set the interrupt and enable <br/> tmr32b0ctl = 0x01; // enable the timer with 0 counters <br/>}< br/> void keyboard_set (void) <br/>{< br/> gpio_init (); <br/> ssp_init (); <br/> keyboardinit (); <br/> timer0init (); <br/>} 
This is the header file for controlling the keyboard board. You only need to call this in the main function:
Num_left4bit = num1; // update the data num1 to be displayed on the left-side digital tube
Breaknum_left (num1 );
Num_right4bit = num2; // update the data num2 to be displayed on the left-side digital tube
Breaknum_right (num2 );

This program was transplanted from the keyboard program on the previous 2103.
2103 key board DRIVER: http://www.jxust3jia1.com/BBS/viewthread.php? Tid = 10536 & extra = Page % 3d2

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.