Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of L432KC-SMT16030 by
Revision 0:af4af28f0975, committed 2017-01-10
- Comitter:
- lmboogaard
- Date:
- Tue Jan 10 11:32:26 2017 +0000
- Commit message:
- test program for Mbed CLI; no SPI rate, no UART, no I2C
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD12864.cpp Tue Jan 10 11:32:26 2017 +0000
@@ -0,0 +1,203 @@
+/*
+ Arduino library v1.0
+ Demo LCD12864 spi
+ www.dfrobot.com
+ Version 0.0
+
+ Update in Februray 26th, 2013
+ Library is applied to Arduino 1.0
+ Having added a "initDriverPin" function that allows users to define the SPI interface
+ Version 1.0
+*/
+#include "mbed.h"
+#include "LCD12864.h"
+
+SPI LCD(PB_5, PB_4, PB_3); //PA_7,PA_6,PA_5 LS7366 Quadrature counter
+DigitalOut CS_LCD(PA_11); // PA_4 = PA8
+
+void LCD12864RSPI::WriteByte(int dat)
+{
+
+ CS_LCD = 1;
+ wait_ms(80); //was 80ms
+ LCD.write(dat); //msbit first
+ CS_LCD = 0;
+}
+
+
+void LCD12864RSPI::WriteCommand(int CMD)
+{
+ int H_data,L_data;
+ H_data = CMD;
+ H_data &= 0xf0; //ÆÁ±ÎµÍ4λµÄÊý¾Ý
+ L_data = CMD; //xxxx0000¸ñʽ
+ L_data &= 0x0f; //ÆÁ±Î¸ß4λµÄÊý¾Ý
+ L_data <<= 4; //xxxx0000¸ñʽ
+ WriteByte(0xf8); //RS=0£¬Ð´ÈëµÄÊÇÖ¸Á
+ WriteByte(H_data);
+ WriteByte(L_data);
+}
+
+
+void LCD12864RSPI::WriteData(int CMD)
+{
+ int H_data,L_data;
+ H_data = CMD;
+ H_data &= 0xf0; //ÆÁ±ÎµÍ4λµÄÊý¾Ý
+ L_data = CMD; //xxxx0000¸ñʽ
+ L_data &= 0x0f; //ÆÁ±Î¸ß4λµÄÊý¾Ý
+ L_data <<= 4; //xxxx0000¸ñʽ
+ WriteByte(0xfa); //RS=1£¬Ð´ÈëµÄÊÇÊý¾Ý
+ WriteByte(H_data);
+ WriteByte(L_data);
+}
+
+
+
+void LCD12864RSPI::Initialise()
+{
+ LCD.format(8,3); //dac 13bit, res = 8192
+ LCD.frequency(10000);
+ CS_LCD = 0;
+ wait_ms(80);;
+
+ WriteCommand(0x30); //¹¦ÄÜÉ趨¿ØÖÆ×Ö
+ WriteCommand(0x0c); //ÏÔʾ¿ª¹Ø¿ØÖÆ×Ö
+ WriteCommand(0x01); //Çå³ýÆÁÄ»¿ØÖÆ×Ö
+ WriteCommand(0x06); //½øÈëÉ趨µã¿ØÖÆ×Ö
+}
+
+
+void LCD12864RSPI::CLEAR(void)
+{
+ WriteCommand(0x30);//
+ WriteCommand(0x01);//Çå³ýÏÔʾ
+}
+
+
+void LCD12864RSPI::DisplayString(int X,int Y, char *ptr,int dat)
+{
+ int i;
+
+ switch(X)
+ {
+ case 0: Y|=0x80;break;
+
+ case 1: Y|=0x90;break;
+
+ case 2: Y|=0x88;break;
+
+ case 3: Y|=0x98;break;
+
+ default: break;
+ }
+
+ WriteCommand(Y); // ¶¨Î»ÏÔʾÆðʼµØÖ·
+
+ for(i=0;i<dat;i++)
+ {
+ WriteData(ptr[i]);//ÏÔʾºº×ÖʱעÒâÂëÖµ£¬Á¬ÐøÁ½¸öÂë±íʾһ¸öºº×Ö
+ }
+}
+
+
+
+void LCD12864RSPI::DisplaySig(int M,int N,int sig)
+{
+ switch(M)
+ {
+ case 0: N|=0x80;break;
+
+ case 1: N|=0x90;break;
+
+ case 2: N|=0x88;break;
+
+ case 3: N|=0x98;break;
+
+ default: break;
+ }
+ WriteCommand(N); // ¶¨Î»ÏÔʾÆðʼµØÖ·
+ WriteData(sig); //Êä³öµ¥¸ö×Ö·û
+ }
+
+
+
+
+void LCD12864RSPI::DrawFullScreen(uchar *p)
+{
+ int ygroup,x,y,i;
+ int temp;
+ int tmp;
+
+ for(ygroup=0;ygroup<64;ygroup++) //дÈëÒº¾§ÉϰëͼÏ󲿷Ö
+ { //дÈë×ø±ê
+ if(ygroup<32)
+ {
+ x=0x80;
+ y=ygroup+0x80;
+ }
+ else
+ {
+ x=0x88;
+ y=ygroup-32+0x80;
+ }
+ WriteCommand(0x34); //дÈëÀ©³äÖ¸ÁîÃüÁî
+ WriteCommand(y); //дÈëyÖá×ø±ê
+ WriteCommand(x); //дÈëxÖá×ø±ê
+ WriteCommand(0x30); //дÈë»ù±¾Ö¸ÁîÃüÁî
+ tmp=ygroup*16;
+ for(i=0;i<16;i++)
+ {
+ temp=p[tmp++];
+ WriteData(temp);
+ }
+ }
+ WriteCommand(0x34); //дÈëÀ©³äÖ¸ÁîÃüÁî
+ WriteCommand(0x36); //ÏÔʾͼÏó
+}
+
+
+
+void LCD12864RSPI::img1(uchar img[])
+{
+ unsigned int i;
+ unsigned char page,column;
+
+ for(page=0xB0;page<0xB4;page++)
+ {
+ WriteCommand(page); //set page address
+ WriteCommand(0x10); //set Column address MSB
+ WriteCommand(0x04); //set column address LSB
+
+ i = (0xB3-page)*128;
+
+ for(column=0;column<128;column++)
+ {
+ WriteData(~img[i+column]);
+ }
+ }
+ WriteCommand(0x34); //дÈëÀ©³äÖ¸ÁîÃüÁî
+ WriteCommand(0x36); //ÏÔʾͼÏó
+}
+
+void LCD12864RSPI::img2(uchar img[])
+{
+ unsigned int i;
+ unsigned char page,column;
+
+ for(page=0xB4;page<0xB8;page++)
+ {
+ WriteCommand(page); //set page address
+ WriteCommand(0x10); //set Column address MSB
+ WriteCommand(0x04); //set column address LSB
+
+ i = (0xB7-page)*128;
+
+ for(column=0;column<128;column++)
+ {
+ WriteData(~img[i+column]);
+ }
+ }
+ WriteCommand(0x34); //дÈëÀ©³äÖ¸ÁîÃüÁî
+ WriteCommand(0x36); //ÏÔʾͼÏó
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD12864.h Tue Jan 10 11:32:26 2017 +0000
@@ -0,0 +1,26 @@
+//Arduino library v1.0
+//Demo LCD12864 spi
+//www.dfrobot.com
+
+#ifndef LCD12864RSPI_h
+#define LCD12864RSPI_h
+#include "mbed.h"
+
+class LCD12864RSPI {
+typedef unsigned char uchar;
+
+public:
+void Initialise(void);
+void WriteByte(int dat);
+void WriteCommand(int CMD);
+void WriteData(int CMD);
+void CLEAR(void);
+void DisplayString(int X,int Y, char *ptr,int dat);
+void DisplaySig(int M,int N,int sig);
+void DrawFullScreen(uchar *p);
+void img1(uchar img[]);
+void img2(uchar img[]);
+
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialIRQ.cpp Tue Jan 10 11:32:26 2017 +0000
@@ -0,0 +1,197 @@
+#include "mbed.h"
+// Serial TX & RX interrupt loopback test using formatted IO - sprintf and sscanf
+// Connect TX to RX (p9 to p10)
+// or can also use USB and type back in the number printed out in a terminal window
+// Sends out ASCII numbers in a loop and reads them back
+// If not the same number LED4 goes on
+// LED1 and LED2 indicate RX and TX interrupt routine activity
+// LED3 changing indicate main loop running
+
+
+Serial device(PA_2, PA_3); // tx, rx
+// Can also use USB and type back in the number printed out in a terminal window
+// Serial monitor_device(USBTX, USBRX);
+DigitalOut led1(PB_3);
+DigitalOut led2(PB_4);
+DigitalOut led3(PB_5);
+DigitalOut led4(PB_6);
+
+#define UART1_IRQn 1
+
+int Serial_main(void);
+void Tx_interrupt();
+void Rx_interrupt();
+void send_line();
+void read_line();
+void NVIC_DisableIRQ(int IRQ);
+void NVIC_EnableIRQ(int IRQ);
+
+// Circular buffers for serial TX and RX data - used by interrupt routines
+const int buffer_size = 255;
+// might need to increase buffer size for high baud rates
+char tx_buffer[buffer_size+1];
+char rx_buffer[buffer_size+1];
+// Circular buffer pointers
+// volatile makes read-modify-write atomic
+volatile int tx_in=0;
+volatile int tx_out=0;
+volatile int rx_in=0;
+volatile int rx_out=0;
+// Line buffers for sprintf and sscanf
+char tx_line[80];
+char rx_line[80];
+
+
+// main test program
+int Serial_main() {
+ int i=0;
+ int rx_i=0;
+ device.baud(9600);
+
+// Setup a serial interrupt function to receive data
+ device.attach(&Rx_interrupt, Serial::RxIrq);
+// Setup a serial interrupt function to transmit data
+ device.attach(&Tx_interrupt, Serial::TxIrq);
+
+// Formatted IO test using send and receive serial interrupts
+// with sprintf and sscanf
+ while (1) {
+// Loop to generate different test values - send value in hex, decimal, and octal and then read back
+ for (i=0; i<0xFFFF; i++) {
+ led3=1;
+// Print ASCII number to tx line buffer in hex
+ sprintf(tx_line,"%x\r\n",i);
+// Copy tx line buffer to large tx buffer for tx interrupt routine
+ send_line();
+// Print ASCII number to tx line buffer in decimal
+ sprintf(tx_line,"%d\r\n",i);
+// Copy tx line buffer to large tx buffer for tx interrupt routine
+ send_line();
+// Print ASCII number to tx line buffer in octal
+ sprintf(tx_line,"%o\r\n",i);
+// Copy tx line buffer to large tx buffer for tx interrupt routine
+ send_line();
+ led3=0;
+
+// Read a line from the large rx buffer from rx interrupt routine
+ read_line();
+// Read ASCII number from rx line buffer
+ sscanf(rx_line,"%x",&rx_i);
+// Check that numbers are the same
+ if (i != rx_i) led4=1;
+// Read a line from the large rx buffer from rx interrupt routine
+ read_line();
+// Read ASCII number from rx line buffer
+ sscanf(rx_line,"%d",&rx_i);
+// Check that numbers are the same
+ if (i != rx_i) led4=1;
+// Read a line from the large rx buffer from rx interrupt routine
+ read_line();
+// Read ASCII number from rx line buffer
+ sscanf(rx_line,"%o",&rx_i);
+// Check that numbers are the same
+ if (i != rx_i) led4=1;
+ }
+ }
+}
+
+
+// Copy tx line buffer to large tx buffer for tx interrupt routine
+void send_line() {
+ int i;
+ char temp_char;
+ bool empty;
+ i = 0;
+// Start Critical Section - don't interrupt while changing global buffer variables
+ NVIC_DisableIRQ(UART1_IRQn);
+ empty = (tx_in == tx_out);
+ while ((i==0) || (tx_line[i-1] != '\n')) {
+// Wait if buffer full
+ if (((tx_in + 1) % buffer_size) == tx_out) {
+// End Critical Section - need to let interrupt routine empty buffer by sending
+ NVIC_EnableIRQ(UART1_IRQn);
+ while (((tx_in + 1) % buffer_size) == tx_out) {
+ }
+// Start Critical Section - don't interrupt while changing global buffer variables
+ NVIC_DisableIRQ(UART1_IRQn);
+ }
+ tx_buffer[tx_in] = tx_line[i];
+ i++;
+ tx_in = (tx_in + 1) % buffer_size;
+ }
+ if (device.writeable() && (empty)) {
+ temp_char = tx_buffer[tx_out];
+ tx_out = (tx_out + 1) % buffer_size;
+// Send first character to start tx interrupts, if stopped
+ device.putc(temp_char);
+ }
+// End Critical Section
+ NVIC_EnableIRQ(UART1_IRQn);
+ return;
+}
+
+
+// Read a line from the large rx buffer from rx interrupt routine
+void read_line() {
+ int i;
+ i = 0;
+// Start Critical Section - don't interrupt while changing global buffer variables
+ NVIC_DisableIRQ(UART1_IRQn);
+// Loop reading rx buffer characters until end of line character
+ while ((i==0) || (rx_line[i-1] != '\r')) {
+// Wait if buffer empty
+ if (rx_in == rx_out) {
+// End Critical Section - need to allow rx interrupt to get new characters for buffer
+ NVIC_EnableIRQ(UART1_IRQn);
+ while (rx_in == rx_out) {
+ }
+// Start Critical Section - don't interrupt while changing global buffer variables
+ NVIC_DisableIRQ(UART1_IRQn);
+ }
+ rx_line[i] = rx_buffer[rx_out];
+ i++;
+ rx_out = (rx_out + 1) % buffer_size;
+ }
+// End Critical Section
+ NVIC_EnableIRQ(UART1_IRQn);
+ rx_line[i-1] = 0;
+ return;
+}
+
+
+// Interupt Routine to read in data from serial port
+void Rx_interrupt() {
+ led1=1;
+// Loop just in case more than one character is in UART's receive FIFO buffer
+// Stop if buffer full
+ while ((device.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
+ rx_buffer[rx_in] = device.getc();
+// Uncomment to Echo to USB serial to watch data flow
+// monitor_device.putc(rx_buffer[rx_in]);
+ rx_in = (rx_in + 1) % buffer_size;
+ }
+ led1=0;
+ return;
+}
+
+
+// Interupt Routine to write out data to serial port
+void Tx_interrupt() {
+ led2=1;
+// Loop to fill more than one character in UART's transmit FIFO buffer
+// Stop if buffer empty
+ while ((device.writeable()) && (tx_in != tx_out)) {
+ device.putc(tx_buffer[tx_out]);
+ tx_out = (tx_out + 1) % buffer_size;
+ }
+ led2=0;
+ return;
+}
+
+void NVIC_EnableIRQ(int IRQ){
+ __enable_irq();
+}
+
+void NVIC_DisableIRQ(int IRQ){
+ __disable_irq();
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Jan 10 11:32:26 2017 +0000
@@ -0,0 +1,189 @@
+#include "mbed.h"
+#include "LCD12864.h"
+
+DigitalOut myled(PB_1);
+DigitalIn Senslev(PA_8);
+
+Serial RFData(PA_2, PA_3);//was PA_2,PA_3
+//Serial pc(USBTX,USBRX);
+SPI QCOUNT(PB_5, PB_4, PB_3);
+Timer Duty;
+Timer Cycle;
+int Td,T;
+LCD12864RSPI mylcd;
+
+float Store[10];
+float Average(float* array);
+char TxtBuf[17] = "Hier staat niets";
+int n = 0;
+//************
+// Circular buffers for serial TX and RX data - used by interrupt routines
+extern const int buffer_size = 255;
+// might need to increase buffer size for high baud rates
+extern char tx_buffer[buffer_size+1];
+extern char rx_buffer[buffer_size+1];
+// Circular buffer pointers
+// volatile makes read-modify-write atomic
+extern volatile int tx_in;
+extern volatile int tx_out;
+extern volatile int rx_in;
+extern volatile int rx_out;
+// Line buffers for sprintf and sscanf
+extern char tx_line[80];
+extern char rx_line[80];
+extern void read_line(void);
+//*************
+void Catch(void)
+{
+ myled=1;
+// Loop just in case more than one character is in UART's receive FIFO buffer
+// Stop if buffer full
+ while ((RFData.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
+ rx_buffer[rx_in] = RFData.getc();
+// Uncomment to Echo to USB serial to watch data flow
+// monitor_device.putc(rx_buffer[rx_in]);
+ rx_in = (rx_in + 1) % buffer_size;
+ }
+ myled=0;
+ return;
+
+}
+/*
+ // get the current time from the terminal
+ struct tm t;
+ printf("Enter current date and time:\n");
+ printf("YYYY MM DD HH MM SS[enter]\n");
+ scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
+ , &t.tm_hour, &t.tm_min, &t.tm_sec);
+
+ // adjust for tm structure required values
+ t.tm_year = t.tm_year - 1900;
+ t.tm_mon = t.tm_mon - 1;
+
+ // set the time
+ set_time(mktime(&t));
+
+ // display the time
+ while(1) {
+ time_t seconds = time(NULL);
+ printf("Time as a basic string = %s", ctime(&seconds));
+ wait(1);
+ }
+
+int tm_sec //seconds after the minute – [0, 60][@1] (public member object)
+int tm_min //minutes after the hour – [0, 59] (public member object)
+int tm_hour //hours since midnight – [0, 23] (public member object)
+int tm_mday //day of the month – [1, 31] (public member object)
+int tm_mon //months since January – [0, 11] (public member object)
+int tm_year //years since 1900 (public member object)
+int tm_wday //days since Sunday – [0, 6] (public member object)
+int tm_yday //days since January 1 – [0, 365]
+int tm_isdst //Daylight Saving Time flag. The value is positive if DST is in effect, zero if
+ //not and negative if no information is available (public member object)
+*/
+struct tm t;
+
+int main()
+{
+ QCOUNT.format(8,0); //dac 13bit, res = 8192
+ QCOUNT.frequency(10000);
+
+ RFData.format(8,SerialBase::None, 1);
+ RFData.baud(4800);
+
+ t.tm_sec = 00;
+ t.tm_min = 17;
+ t.tm_hour = 15;
+ t.tm_mday = 2;
+ t.tm_mon = 0;
+ t.tm_year = 2017 - 1900;
+ t.tm_wday = 1;
+ t.tm_yday = 2;
+ t.tm_isdst = 1;
+
+ char timeBuf[30];
+ char *ptimeBuf = timeBuf;
+ char Buf[16];
+ char *pBuf = Buf;
+
+ set_time(mktime(&t));
+ time_t seconds = time(NULL);
+ sprintf(ptimeBuf, "%s", ctime(&seconds));
+
+ mylcd.Initialise(); //was initialise
+ mylcd.CLEAR();
+ mylcd.DisplayString(0, 0, "Temperatuur = ", 14);
+ mylcd.DisplayString(2, 0, ptimeBuf, 11);
+ mylcd.DisplayString(3, 0, ptimeBuf + 11 , 13);
+
+ wait_ms(100);
+
+ int i = 0;
+ strcpy(rx_buffer, TxtBuf);
+ RFData.attach(&Catch, Serial::RxIrq);
+ __enable_irq();
+ rx_in = 0;
+ while(1) {
+
+/*
+ while (Senslev) {}; //synchrounise level rise
+
+ while (!Senslev) {};
+ Duty.start();
+ Cycle.start();
+
+
+ while (Senslev) {};
+ Duty.stop();
+ Td = Duty.read_us();
+ Duty.reset();
+
+ while (!Senslev) {};
+ Cycle.stop();
+ T = Cycle.read_us();
+ Cycle.reset();
+
+ Store[i] = 212.77f * Td/T - 68.08f;
+ wait(1);//was 10
+ myled = !myled;
+ i++;
+
+ //do temp calculation
+ //......
+ if(i == 10) {
+
+
+ float temp = Average(Store);
+
+
+ sprintf(pBuf, "%2.2f", temp);
+ *(pBuf + 5) = ' '; //symbol graden chinees
+ *(pBuf + 6) = 'C';
+ *(pBuf + 7) = '\0';
+
+ time_t seconds = time(NULL);
+ sprintf(ptimeBuf, "%s", ctime(&seconds));
+ mylcd.DisplayString(1, 0, pBuf, strlen(pBuf));
+// mylcd.DisplayString(2, 0, ptimeBuf, 11);
+ mylcd.DisplayString(3, 0, ptimeBuf + 11 , 13);
+
+ i = 0;
+ }
+*/
+
+
+// read_line();
+ mylcd.DisplayString(2, 0, rx_line, 16);//strlen(pBuf));
+
+ }
+}
+
+float Average(float* array)
+{
+ float Result = 0;
+
+ for(int i = 0; i < 10; i++)
+ Result += *(array + i);
+
+ return Result/10;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Jan 10 11:32:26 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9baf128c2fab \ No newline at end of file
