just test of mbed cli and stlink

Dependencies:   mbed ShiftReg

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ShiftReg.h"
00003  #include <iostream>
00004  #include <string>
00005  
00006  using namespace std;
00007  
00008 
00009 //------------------------------------
00010 // Hyperterminal configuration
00011 // 9600 bauds, 8-bit data, no parity
00012 //------------------------------------
00013 Serial pc(SERIAL_TX, SERIAL_RX);
00014 
00015 DigitalOut myled(LED1);
00016 
00017 Ticker seg; //7segment timeloop
00018 
00019 int cycle;  //7segment timeloop global variable
00020 int segment [] = {0b00000011,0b10011111,0b00100101,0b00001101,0b10011001,0b01001001,0b01000001,0b00011111,0b00000001,0b00011001,0b11111111};   //7segment coding
00021 
00022 ShiftReg   HC595(PA_9, PB_5, PA_8);   //shiftreger pinout
00023 int poz4;  //char for etch 7 segment
00024 int poz3;
00025 int poz2;
00026 int poz1;
00027 int value = 0;
00028 char u;
00029 string sentence;
00030 
00031 PwmOut buzzer(PA_5);
00032 
00033 void transformer ()    // int to 4x7
00034 {
00035     poz4 = value / 1000;
00036     poz3 = (value - (poz4 * 1000))/100;
00037     poz2 = (value - (poz4 * 1000) - (poz3 * 100))/10;
00038     poz1 = (value - (poz4 * 1000) - (poz3 * 100) - (poz2 * 10))%10;
00039 }
00040 
00041 void interr()          //Shift register update for each 7segment
00042 {
00043     transformer();   //update
00044     cycle++;
00045     if (cycle == 1) {
00046         HC595.ShiftByte(segment[poz4], ShiftReg::LSBFirst);     //segment   (Prvy v poradi)
00047         HC595.ShiftByte(0b00000001, ShiftReg::MSBFirst);    //Pozicia  (druhy v poradi)
00048         HC595.Latch();
00049     }
00050     if (cycle == 2) {
00051         HC595.ShiftByte(segment[poz3] - 1, ShiftReg::LSBFirst);     //segment   (Prvy v poradi)
00052         HC595.ShiftByte(0b00000010, ShiftReg::MSBFirst);    //Pozicia  (druhy v poradi)
00053         HC595.Latch();
00054     }
00055     if (cycle == 3) {
00056         HC595.ShiftByte(segment[poz2], ShiftReg::LSBFirst);     //segment   (Prvy v poradi)
00057         HC595.ShiftByte(0b00000100, ShiftReg::MSBFirst);    //Pozicia  (druhy v poradi)
00058         HC595.Latch();
00059     }
00060     if (cycle == 4 ) {
00061         HC595.ShiftByte(segment[poz1], ShiftReg::LSBFirst);     //segment   (Prvy v poradi)
00062         HC595.ShiftByte(0b00001000, ShiftReg::MSBFirst);    //Pozicia  (druhy v poradi)
00063         HC595.Latch();
00064     }
00065     if (cycle == 5 ) {
00066         cycle = 0;
00067         HC595.ShiftByte(0x00, ShiftReg::MSBFirst);
00068         HC595.Latch();
00069     }
00070 }
00071 
00072 
00073 
00074 
00075 int main()
00076 {
00077 
00078     buzzer.period_ms(100);
00079     buzzer.pulsewidth_ms(99);
00080     seg.attach_us(&interr, 2000); //
00081     value = 0;
00082     cout << "OK this looks this is firs lin in C++ code :)" << endl;
00083     cin >> sentence ;
00084     cout << sentence << endl;
00085     while(1) {
00086         //cin >> sentence;
00087         char a = cin.get();
00088         cin >> sentence;
00089         if (a == 13) {
00090         cout << sentence;
00091         pc.printf("\n"); }
00092         else if (a == 32){
00093         cout << sentence ;
00094         pc.printf(" "); }
00095         else {
00096         cout << sentence; }
00097         buzzer.write(0.5);
00098         wait(0.2);
00099         buzzer.write(0);
00100        
00101     }//end of while (1)
00102 
00103 }//end off main
00104 
00105 
00106