![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
bgf
main.cpp@0:a07e5a4c94bb, 2014-12-20 (annotated)
- Committer:
- pradeepvk2208
- Date:
- Sat Dec 20 12:17:15 2014 +0000
- Revision:
- 0:a07e5a4c94bb
dfghj
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pradeepvk2208 | 0:a07e5a4c94bb | 1 | #include "mbed.h" |
pradeepvk2208 | 0:a07e5a4c94bb | 2 | |
pradeepvk2208 | 0:a07e5a4c94bb | 3 | SPI spi(PTD6, PTD7, PTD5); // mosi, miso, sclk |
pradeepvk2208 | 0:a07e5a4c94bb | 4 | DigitalOut cs(D9); |
pradeepvk2208 | 0:a07e5a4c94bb | 5 | |
pradeepvk2208 | 0:a07e5a4c94bb | 6 | |
pradeepvk2208 | 0:a07e5a4c94bb | 7 | int main() |
pradeepvk2208 | 0:a07e5a4c94bb | 8 | { |
pradeepvk2208 | 0:a07e5a4c94bb | 9 | |
pradeepvk2208 | 0:a07e5a4c94bb | 10 | |
pradeepvk2208 | 0:a07e5a4c94bb | 11 | |
pradeepvk2208 | 0:a07e5a4c94bb | 12 | // Setup the spi for 8 bit data, high steady state clock, |
pradeepvk2208 | 0:a07e5a4c94bb | 13 | // second edge capture, with a 1MHz clock rate |
pradeepvk2208 | 0:a07e5a4c94bb | 14 | spi.format(8,3); |
pradeepvk2208 | 0:a07e5a4c94bb | 15 | spi.frequency(1000000); |
pradeepvk2208 | 0:a07e5a4c94bb | 16 | |
pradeepvk2208 | 0:a07e5a4c94bb | 17 | cs = 0; |
pradeepvk2208 | 0:a07e5a4c94bb | 18 | spi.write(0x80); // set write register to seconds |
pradeepvk2208 | 0:a07e5a4c94bb | 19 | spi.write(0x01); // send value of zero |
pradeepvk2208 | 0:a07e5a4c94bb | 20 | |
pradeepvk2208 | 0:a07e5a4c94bb | 21 | //cs=1; |
pradeepvk2208 | 0:a07e5a4c94bb | 22 | //cs=0; |
pradeepvk2208 | 0:a07e5a4c94bb | 23 | spi.write(0x81); |
pradeepvk2208 | 0:a07e5a4c94bb | 24 | spi.write(0x58); //write reg to minutes |
pradeepvk2208 | 0:a07e5a4c94bb | 25 | |
pradeepvk2208 | 0:a07e5a4c94bb | 26 | //cs=1; |
pradeepvk2208 | 0:a07e5a4c94bb | 27 | //cs=0; |
pradeepvk2208 | 0:a07e5a4c94bb | 28 | |
pradeepvk2208 | 0:a07e5a4c94bb | 29 | spi.write(0x82); |
pradeepvk2208 | 0:a07e5a4c94bb | 30 | spi.write(0x01); //write reg to hours (00-23) |
pradeepvk2208 | 0:a07e5a4c94bb | 31 | |
pradeepvk2208 | 0:a07e5a4c94bb | 32 | spi.write(0x83); |
pradeepvk2208 | 0:a07e5a4c94bb | 33 | spi.write(0x01); //write reg to day (1-7) |
pradeepvk2208 | 0:a07e5a4c94bb | 34 | |
pradeepvk2208 | 0:a07e5a4c94bb | 35 | spi.write(0x84); |
pradeepvk2208 | 0:a07e5a4c94bb | 36 | spi.write(0x01); //write reg to date(01-31) |
pradeepvk2208 | 0:a07e5a4c94bb | 37 | |
pradeepvk2208 | 0:a07e5a4c94bb | 38 | spi.write(0x85); |
pradeepvk2208 | 0:a07e5a4c94bb | 39 | spi.write(0x01); //write reg to mnth(01-12) |
pradeepvk2208 | 0:a07e5a4c94bb | 40 | |
pradeepvk2208 | 0:a07e5a4c94bb | 41 | spi.write(0x86); |
pradeepvk2208 | 0:a07e5a4c94bb | 42 | spi.write(0x01); //write reg to year(00-99) |
pradeepvk2208 | 0:a07e5a4c94bb | 43 | |
pradeepvk2208 | 0:a07e5a4c94bb | 44 | cs=1; |
pradeepvk2208 | 0:a07e5a4c94bb | 45 | // Receive the contents of the seconds register |
pradeepvk2208 | 0:a07e5a4c94bb | 46 | |
pradeepvk2208 | 0:a07e5a4c94bb | 47 | for(int i=0;i<1000000;i++){ |
pradeepvk2208 | 0:a07e5a4c94bb | 48 | |
pradeepvk2208 | 0:a07e5a4c94bb | 49 | |
pradeepvk2208 | 0:a07e5a4c94bb | 50 | cs=0; |
pradeepvk2208 | 0:a07e5a4c94bb | 51 | spi.write(0x00); // set read register to seconds |
pradeepvk2208 | 0:a07e5a4c94bb | 52 | int seconds = spi.write(0x00); // read the value by sending dummy byte |
pradeepvk2208 | 0:a07e5a4c94bb | 53 | |
pradeepvk2208 | 0:a07e5a4c94bb | 54 | cs=1; |
pradeepvk2208 | 0:a07e5a4c94bb | 55 | cs=0; |
pradeepvk2208 | 0:a07e5a4c94bb | 56 | |
pradeepvk2208 | 0:a07e5a4c94bb | 57 | spi.write(0x01); |
pradeepvk2208 | 0:a07e5a4c94bb | 58 | int minutes =spi.write(0x01); |
pradeepvk2208 | 0:a07e5a4c94bb | 59 | |
pradeepvk2208 | 0:a07e5a4c94bb | 60 | cs=1; |
pradeepvk2208 | 0:a07e5a4c94bb | 61 | cs=0; |
pradeepvk2208 | 0:a07e5a4c94bb | 62 | |
pradeepvk2208 | 0:a07e5a4c94bb | 63 | spi.write(0x02); |
pradeepvk2208 | 0:a07e5a4c94bb | 64 | int hours =spi.write(0x01); |
pradeepvk2208 | 0:a07e5a4c94bb | 65 | |
pradeepvk2208 | 0:a07e5a4c94bb | 66 | cs=1; |
pradeepvk2208 | 0:a07e5a4c94bb | 67 | cs=0; |
pradeepvk2208 | 0:a07e5a4c94bb | 68 | |
pradeepvk2208 | 0:a07e5a4c94bb | 69 | spi.write(0x03); |
pradeepvk2208 | 0:a07e5a4c94bb | 70 | int day =spi.write(0x01); |
pradeepvk2208 | 0:a07e5a4c94bb | 71 | |
pradeepvk2208 | 0:a07e5a4c94bb | 72 | cs=1; |
pradeepvk2208 | 0:a07e5a4c94bb | 73 | cs=0; |
pradeepvk2208 | 0:a07e5a4c94bb | 74 | |
pradeepvk2208 | 0:a07e5a4c94bb | 75 | spi.write(0x04); |
pradeepvk2208 | 0:a07e5a4c94bb | 76 | int date =spi.write(0x01); |
pradeepvk2208 | 0:a07e5a4c94bb | 77 | |
pradeepvk2208 | 0:a07e5a4c94bb | 78 | cs=1; |
pradeepvk2208 | 0:a07e5a4c94bb | 79 | cs=0; |
pradeepvk2208 | 0:a07e5a4c94bb | 80 | |
pradeepvk2208 | 0:a07e5a4c94bb | 81 | spi.write(0x05); |
pradeepvk2208 | 0:a07e5a4c94bb | 82 | int mnth =spi.write(0x01); |
pradeepvk2208 | 0:a07e5a4c94bb | 83 | |
pradeepvk2208 | 0:a07e5a4c94bb | 84 | cs=1; |
pradeepvk2208 | 0:a07e5a4c94bb | 85 | cs=0; |
pradeepvk2208 | 0:a07e5a4c94bb | 86 | |
pradeepvk2208 | 0:a07e5a4c94bb | 87 | spi.write(0x06); |
pradeepvk2208 | 0:a07e5a4c94bb | 88 | int year =spi.write(0x01); |
pradeepvk2208 | 0:a07e5a4c94bb | 89 | |
pradeepvk2208 | 0:a07e5a4c94bb | 90 | //This cs=1 is to make the chipselect line high to "deselect" the slave in our case RTC |
pradeepvk2208 | 0:a07e5a4c94bb | 91 | cs = 1; |
pradeepvk2208 | 0:a07e5a4c94bb | 92 | |
pradeepvk2208 | 0:a07e5a4c94bb | 93 | //This printf function is to check the timestamp function in the terminal output |
pradeepvk2208 | 0:a07e5a4c94bb | 94 | printf("timestamp as = %X : %X : %X : %X : %X : %X : %X\n\r", year,mnth,date,day,hours,minutes,seconds); |
pradeepvk2208 | 0:a07e5a4c94bb | 95 | } |
pradeepvk2208 | 0:a07e5a4c94bb | 96 | |
pradeepvk2208 | 0:a07e5a4c94bb | 97 | |
pradeepvk2208 | 0:a07e5a4c94bb | 98 | |
pradeepvk2208 | 0:a07e5a4c94bb | 99 | return 0; |
pradeepvk2208 | 0:a07e5a4c94bb | 100 | |
pradeepvk2208 | 0:a07e5a4c94bb | 101 | } |