Advertisement
Guest User

New item

a guest
Oct 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. /*-----------------------------------------------------------------------------
  2. * Name: main.c
  3. * Purpose: Toggling_LEDs application
  4. * Author: Student
  5. *----------------------------------------------------------------------------*/
  6. #include "MKL25Z4.h" /*Device header*/
  7. #define LED_1 0
  8. #define SW_1 0
  9.  
  10. #define LED_2 1
  11. #define SW_2 1
  12.  
  13. #define LED_3 2
  14. #define SW_3 2
  15.  
  16. #define LED_4 3
  17. #define SW_4 3
  18.  
  19. void dMs ( int n) {
  20. int i;
  21. int j;
  22. for( i = 0 ; i < n; i++)
  23. for(j = 0; j < 7000; j++) {}
  24. }
  25.  
  26. int main() {
  27. //Enable clock for Port B and Port D
  28. SIM->SCGC5|=SIM_SCGC5_PORTB(1)|SIM_SCGC5_PORTD(1);
  29.  
  30. //Configure Port B pin 0 as GPIO, Output
  31. PORTB->PCR[LED_1]|=PORT_PCR_MUX(1);
  32. PTB->PDDR|=(1<<LED_1);
  33.  
  34. PORTB->PCR[LED_2]|=PORT_PCR_MUX(1);
  35. PTB->PDDR|=(1<<LED_2);
  36.  
  37.  
  38. PORTB->PCR[LED_3]|=PORT_PCR_MUX(1);
  39. PTB->PDDR|=(1<<LED_3);
  40.  
  41. PORTB->PCR[LED_4]|=PORT_PCR_MUX(1);
  42. PTB->PDDR|=(1<<LED_4);
  43.  
  44.  
  45. //Configure Port D pin 0 as GPIO, Input, Pull-up resistor
  46. PORTD->PCR[SW_1]|=PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(0);
  47. PTD->PDDR&=~(1<<SW_1);
  48.  
  49. PORTD->PCR[SW_2]|=PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(0);
  50. PTD->PDDR&=~(1<<SW_2);
  51.  
  52.  
  53. PORTD->PCR[SW_3]|=PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(0);
  54. PTD->PDDR&=~(1<<SW_3);
  55.  
  56.  
  57. PORTD->PCR[SW_4]|=PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(0);
  58. PTD->PDDR&=~(1<<SW_4);
  59.  
  60.  
  61.  
  62. while(1){
  63.  
  64.  
  65. if( (PTD->PDIR & (1<<SW_1))==0){
  66. dMs(15);
  67. if(PTB->PDOR & (1<<LED_1))
  68. PTB->PDOR&=~(1<<LED_1);
  69. else
  70. PTB->PDOR|=(1<<LED_1);
  71.  
  72.  
  73. while((PTD->PDIR & (1<<SW_1))==0); //Wait for SW_1 release
  74. }
  75.  
  76. if( (PTD->PDIR & (1<<SW_2))==0){
  77. dMs(15);
  78. if(PTB->PDOR & (1<<LED_2))
  79. PTB->PDOR&=~(1<<LED_2);
  80. else
  81. PTB->PDOR|=(1<<LED_2);
  82.  
  83. while((PTD->PDIR & (1<<SW_2))==0); //Wait for SW_2 release
  84. }
  85.  
  86. if( (PTD->PDIR & (1<<SW_3))==0){
  87. dMs(15);
  88. if(PTB->PDOR & (1<<LED_3))
  89. PTB->PDOR&=~(1<<LED_3);
  90. else
  91. PTB->PDOR|=(1<<LED_3);
  92.  
  93. while((PTD->PDIR & (1<<SW_3))==0); //Wait for SW_3 release
  94. }
  95.  
  96. if( (PTD->PDIR & (1<<SW_4))==0){
  97. dMs(15);
  98. if(PTB->PDOR & (1<<LED_4))
  99. PTB->PDOR&=~(1<<LED_4);
  100. else
  101. PTB->PDOR|=(1<<LED_4);
  102.  
  103. while((PTD->PDIR & (1<<SW_4))==0); //Wait for SW_4 release
  104. }
  105.  
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement