Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mbed.h"
- #define LED1 (1 << 18)
- #define LED2 (1 << 20)
- #define LED3 (1 << 21)
- #define LED4 (1 << 23)
- #define pin21 (1 << 5)
- #define pin22 (1 << 4)
- #define pin23 (1 << 3)
- #define SBIT_WordLength 0x00u
- #define SBIT_DLAB 0x07u
- #define SBIT_FIFO 0x00u
- #define SBIT_RxFIFO 0x01u
- #define SBIT_TxFIFO 0x02u
- #define SBIT_THRE 0x05u
- unsigned int* pinsel0 = reinterpret_cast<unsigned int*>(0x4002C000);
- unsigned int* pinsel4 = reinterpret_cast<unsigned int*>(0x4002C010);
- unsigned int* pinmode0 = reinterpret_cast<unsigned int*>(0x4002C040);
- unsigned int* pinmode4 = reinterpret_cast<unsigned int*>(0x4002C050);
- unsigned int* fio2dir0 = reinterpret_cast<unsigned int*>(0x2009C040);
- unsigned int* io2intenr = reinterpret_cast<unsigned int*>(0x400280B0);
- unsigned int* io2intstatr = reinterpret_cast<unsigned int*>(0x400280A4);
- unsigned int* io2intclr = reinterpret_cast<unsigned int*>(0x400280AC);
- unsigned int* uart1FCR = reinterpret_cast<unsigned int*>(0x40010008);
- unsigned int* uart1LCR = reinterpret_cast<unsigned int*>(0x4001000C);
- unsigned int* uart1DLL = reinterpret_cast<unsigned int*>(0x40010000);
- unsigned int* uart1DLM = reinterpret_cast<unsigned int*>(0x40010004);
- unsigned int* uart1THR = reinterpret_cast<unsigned int*>(0x40010000);
- void EINT3_init (void) {
- *pinsel4 &= ~((1 << 7) | (1 << 6));
- *pinmode4 |= (1 << 7) | (1 << 6);
- *pinsel4 &= ~((1 << 9) | (1 << 8));
- *pinmode4 |= (1 << 9) | (1 << 8);
- *pinsel4 &= ~((1 << 11) | (1 << 10));
- *pinmode4 |= (1 << 11) | (1 << 10);
- *fio2dir0 &= ~pin21;
- *fio2dir0 &= ~pin22;
- *fio2dir0 &= ~pin23;
- *io2intenr |= (pin21);
- *io2intenr |= (pin22);
- *io2intenr |= (pin23);
- NVIC_EnableIRQ(EINT3_IRQn);
- }
- //EINT3 Handler
- extern "C" void EINT3_IRQHandler(void) __irq {
- if ((*io2intstatr & pin21) == pin21) {
- *io2intclr = (pin21);
- LPC_GPIO1->FIOSET |= LED1;
- wait(0.01);
- LPC_GPIO1->FIOCLR |= LED1;
- return;
- }
- if ((*io2intstatr & pin22) == pin22) {
- *io2intclr = pin22;
- LPC_GPIO1->FIOSET |= LED2;
- wait(0.01);
- LPC_GPIO1->FIOCLR |= LED2;
- return;
- }
- if ((*io2intstatr & pin23) == pin23) {
- *io2intclr = pin23;
- LPC_GPIO1->FIOSET |= LED3;
- wait(0.01);
- LPC_GPIO1->FIOCLR |= LED3;
- return;
- }
- }
- void initUart1(unsigned int baudrate) {
- *pinsel0 &= ~0xF0000000;
- *pinsel0 |= 0x40000000;
- *pinmode0 |= 0xC0000000;
- *uart1FCR = (1<<SBIT_FIFO) | (1<<SBIT_RxFIFO) | (1<<SBIT_TxFIFO);
- *uart1LCR = (0x03<<SBIT_WordLength) | (1<<SBIT_DLAB);
- int _baudrate = ((96000000/4) / (16 * baudrate));
- *uart1DLL = _baudrate & 0xFF;
- *uart1DLM = (_baudrate << 0x08) & 0xFF;
- *uart1LCR &= ~(1<<SBIT_DLAB);
- }
- void waitDataCleared() {
- while((*uart1LCR & (~(1<<SBIT_THRE))) == 0) { wait(0.000001); }
- }
- void sendCharUart1(char data) {
- waitDataCleared();
- *uart1THR = data;
- }
- void sendIntUart1(int data) {
- waitDataCleared();
- *uart1THR = (data & 0xFF);
- }
- /*
- SPI spi(p5, p6, p7);
- DigitalOut cs(p8);
- */
- int main() {
- //set led 1 and 2 pins as output
- LPC_GPIO1->FIODIR |= LED1 | LED2 | LED3 | LED4;
- LPC_GPIO1->FIOSET |= LED4;
- EINT3_init();
- initUart1(9600);
- wait(2);
- sendCharUart1(0x76);
- wait(0.01);
- /*
- cs = 1;
- spi.format(8,3);
- spi.frequency(250000);
- cs = 0;
- spi.write(0x76);
- wait(0.02);
- cs = 1;
- */
- while(1) {
- sendCharUart1(0x7B);
- wait(0.01);
- sendIntUart1(63);
- wait(0.01);
- sendCharUart1(0x7C);
- wait(0.01);
- sendIntUart1(91);
- wait(0.01);
- sendCharUart1(0x7D);
- wait(0.01);
- sendIntUart1(63);
- wait(0.01);
- sendCharUart1(0x7E);
- wait(0.01);
- sendIntUart1(91);
- wait(0.01);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement