Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. /* Header file with all the essential definitions for a given type of MCU */
  2. #include "MK60D10.h"
  3.  
  4. /* Macros for bit-level registers manipulation */
  5. #define GPIO_PIN_MASK 0x1Fu
  6. #define GPIO_PIN(x) (((1)<<(x & GPIO_PIN_MASK)))
  7.  
  8. /* Constants specifying delay loop duration */
  9. #define DELAY_T 200000
  10.  
  11. /* Mapping of LEDs and buttons to specific port pins: */
  12. // Note: only D9, SW3 and SW5 are used in this sample app
  13. #define LED_D9 0x20 // Port B, bit 5
  14. #define LED_D10 0x10 // Port B, bit 4
  15. #define LED_D11 0x8 // Port B, bit 3
  16. #define LED_D12 0x4 // Port B, bit 2
  17.  
  18. #define BTN_SW2 0x400 // Port E, bit 10
  19. #define BTN_SW3 0x1000 // Port E, bit 12
  20. #define BTN_SW4 0x8000000 // Port E, bit 27
  21. #define BTN_SW5 0x4000000 // Port E, bit 26
  22. #define BTN_SW6 0x800 // Port E, bit 11
  23.  
  24. int pressed_up = 0, pressed_down = 0;
  25. unsigned int compare = 0x1000;
  26.  
  27. unsigned char c; // prijaty znak
  28. unsigned char prompt[7] = " login>"; // text vyzvy
  29.  
  30. /* A delay function */
  31. void delay(long long bound) {
  32.  
  33. long long i;
  34. for(i=0;i<bound;i++);
  35. }
  36.  
  37. /* Initialize the MCU - basic clock settings, turning the watchdog off */
  38. void MCUInit(void) {
  39. MCG_C4 |= ( MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS(0x01) );
  40. SIM_CLKDIV1 |= SIM_CLKDIV1_OUTDIV1(0x00);
  41. WDOG_STCTRLH &= ~WDOG_STCTRLH_WDOGEN_MASK;
  42. }
  43.  
  44. void PortsInit(void)
  45. {
  46. /* Turn on all port clocks */
  47. SIM->SCGC5 = SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTE_MASK;
  48.  
  49. /* Set corresponding PTB pins (connected to LED's) for GPIO functionality */
  50. PORTB->PCR[5] = PORT_PCR_MUX(0x01); // D9
  51. PORTB->PCR[4] = PORT_PCR_MUX(0x01); // D10
  52. PORTB->PCR[3] = PORT_PCR_MUX(0x01); // D11
  53. PORTB->PCR[2] = PORT_PCR_MUX(0x01); // D12
  54.  
  55. PORTE->PCR[10] = PORT_PCR_MUX(0x01); // SW2
  56. PORTE->PCR[12] = PORT_PCR_MUX(0x01); // SW3
  57. PORTE->PCR[27] = PORT_PCR_MUX(0x01); // SW4
  58. PORTE->PCR[26] = PORT_PCR_MUX(0x01); // SW5
  59. PORTE->PCR[11] = PORT_PCR_MUX(0x01); // SW6
  60.  
  61. /* Change corresponding PTB port pins as outputs */
  62. PTB->PDDR = GPIO_PDDR_PDD(0x3C);
  63. PTB->PDOR |= GPIO_PDOR_PDO(0x3C); // turn all LEDs OFF
  64. }
  65.  
  66. /* Inicializace pinu pro vysilani a prijem pres UART - RX a TX */
  67. void PinInit(void) {
  68. SIM->SOPT2 |= SIM_SOPT2_UART0SRC(0x01);
  69. SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK; // Zapnout hodiny pro PORTA a PORTB
  70. SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;
  71.  
  72. PORTB->PCR[1] = ( 0 | PORT_PCR_MUX(0x02) ); // UART0_TX
  73. PORTB->PCR[2] = ( 0 | PORT_PCR_MUX(0x02) ); // UART0_RX
  74.  
  75. PORTB->PCR[13] = ( 0|PORT_PCR_MUX(0x01) ); // Beeper (PTB13)
  76. PTB->PDDR = GPIO_PDDR_PDD( 0x2000 ); // "1" znamena, ze pin bude vystupni
  77. }
  78.  
  79. /* Inicializace UART - nastaveni prenosove rychlosti 115200Bd, 8 bitu, bez parity */
  80. void UART0Init(void) {
  81. /*UART0->C2 &= ~(UART0_C2_TE_MASK | UART0_C2_RE_MASK);
  82.  
  83. UART0->BDH = 0x00;
  84. UART0->BDL = 0x1A; // Baud rate 115 200 Bd, 1 stop bit
  85. UART0->C4 = 0x0F; // Oversampling ratio 16, match address mode disabled
  86.  
  87. UART0->C1 = 0x00; // 8 data bitu, bez parity
  88. UART0->C3 = 0x00;
  89. UART0->MA1 = 0x00; // no match address (mode disabled in C4)
  90. UART0->MA2 = 0x00; // no match address (mode disabled in C4)
  91. UART0->S1 |= 0x1F;
  92. UART0->S2 |= 0xC0;
  93.  
  94. UART0->C2 |= ( UART0_C2_TE_MASK | UART0_C2_RE_MASK ); // Zapnout vysilac i prijimac*/
  95. }
  96.  
  97. /* vyslani jednoho znaku (ch) pres UART - funkce vycka, az je vysilaci buffer prazdny, pak posle */
  98. /*void SendCh(char ch) {
  99. while(!(UART0->S1 & UART0_S1_TDRE_MASK) && !(UART0->S1 & UART0_S1_TC_MASK) );
  100. UART0->D = ch;
  101. }
  102. */
  103. /* prijeti jednoho znaku pres UART - funkce ceka na prichozi znak a ten vrati jako vysledek */
  104. /* !!!!! FUNKCI DOPLNTE */
  105. /*
  106. char ReceiveCh(void) {
  107. while(!(UART0->S1 & UART0_S1_RDRF_MASK));
  108. return UART0->D;
  109. }
  110. */
  111.  
  112. /* vysilani retezce ukonceneho 0 */
  113. void SendStr(char *s) {
  114.  
  115. int i = 0;
  116. while (s[i]!=0){
  117. //SendCh(s[i++]);
  118. }
  119. }
  120.  
  121. void RTC_IRQHandler(void) {
  122.  
  123. if((RTC_SR & RTC_SR_TIF_MASK)== 0x01)
  124. {
  125.  
  126. RTC_SR &= 0x07; //clear TCE, or RTC_TSR can not be written
  127. RTC_TSR = 0x00000000; //clear TIF
  128. }
  129. else if((RTC_SR & RTC_SR_TOF_MASK) == 0x02)
  130. {
  131.  
  132. RTC_SR &= 0x07; //clear TCE, or RTC_TSR can not be written
  133. RTC_TSR = 0x00000000; //clear TOF
  134. }
  135. else if((RTC_SR & RTC_SR_TAF_MASK) == 0x04)
  136. {
  137. GPIOB_PDOR ^= LED_D9; // invert D9 state
  138.  
  139. RTC_TAR += 5;// Write new alarm value, to generate an alarm every second add 1
  140. }
  141. else
  142. {
  143.  
  144. }
  145. return;
  146. }
  147.  
  148. void RTCInit(void)
  149. {
  150.  
  151. int i;
  152.  
  153. /*enable the clock to SRTC module register space*/
  154. SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;
  155.  
  156. /*Only VBAT_POR has an effect on the SRTC, RESET to the part does not, so you must manually reset the SRTC to make sure everything is in a known state*/
  157. /*clear the software reset bit*/
  158. RTC_CR = RTC_CR_SWR_MASK;
  159. RTC_CR &= ~RTC_CR_SWR_MASK;
  160.  
  161. /*Enable the interrupt*/
  162. NVIC_EnableIRQ(RTC_IRQn);
  163.  
  164. /*Enable the oscillator*/
  165. RTC_CR |= RTC_CR_OSCE_MASK;
  166.  
  167.  
  168. /*Wait to all the 32 kHz to stabilize, refer to the crystal startup time in the crystal datasheet*/
  169. delay(6000000);
  170.  
  171. /*Set time compensation parameters*/
  172. RTC_TCR = 0;
  173.  
  174. /*Configure the timer seconds and alarm registers*/
  175. RTC_TSR = 0;
  176. RTC_TAR = 0;
  177.  
  178. /*Enable the counter*/
  179. //RTC_SR |= RTC_SR_TCE_MASK;
  180.  
  181.  
  182. }
  183. void RTCStop(void){
  184. BITBAND_REG(RTC->SR, RTC_SR_TCE_SHIFT) = 0;
  185. }
  186. void RTCStart(void){
  187. BITBAND_REG(RTC->SR, RTC_SR_TCE_SHIFT) = 1;
  188. }
  189.  
  190. void RTCSetTime(int seconds){
  191. RTC->TSR = seconds;
  192. }
  193.  
  194. void RTCSetAlarm(int seconds){
  195. RTC->TAR = seconds;
  196. }
  197.  
  198.  
  199. int main(void)
  200. {
  201. MCUInit();
  202. PortsInit();
  203. long long a = RTC->CR;
  204.  
  205. RTCInit();
  206.  
  207.  
  208.  
  209. //GPIOB_PDOR ^= LED_D9; // invert D9 state
  210.  
  211. while (1) {
  212. SendStr("fuckyou");
  213. /* // pressing the up button decreases the compare value,
  214. // i.e. the compare event will occur more often;
  215. // testing pressed_up avoids uncontrolled modification
  216. // of compare if the button is hold.
  217. if (!pressed_up && !(GPIOE_PDIR & BTN_SW5))
  218. {
  219. pressed_up = 1;
  220. compare -= 0x40;
  221. }
  222. else if (GPIOE_PDIR & BTN_SW5) pressed_up = 0;
  223. // pressing the down button increases the compare value,
  224. // i.e. the compare event will occur less often;
  225. if (!pressed_down && !(GPIOE_PDIR & BTN_SW3))
  226. {
  227. pressed_down = 1;
  228. compare += 0x40;
  229. }
  230. else if (GPIOE_PDIR & BTN_SW3) pressed_down = 0;
  231. // some limits - in order to keep the LED blinking reasonable*/
  232.  
  233. }
  234.  
  235. return 0;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement