Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-----------------------------------------------------------------------------
- * Name: main.c
- * Purpose: Toggling_LEDs application
- * Author: Student
- *----------------------------------------------------------------------------*/
- #include "MKL25Z4.h" /*Device header*/
- #define LED_1 0
- #define SW_1 0
- #define LED_2 1
- #define SW_2 1
- #define LED_3 2
- #define SW_3 2
- #define LED_4 3
- #define SW_4 3
- void dMs ( int n) {
- int i;
- int j;
- for( i = 0 ; i < n; i++)
- for(j = 0; j < 7000; j++) {}
- }
- int main() {
- //Enable clock for Port B and Port D
- SIM->SCGC5|=SIM_SCGC5_PORTB(1)|SIM_SCGC5_PORTD(1);
- //Configure Port B pin 0 as GPIO, Output
- PORTB->PCR[LED_1]|=PORT_PCR_MUX(1);
- PTB->PDDR|=(1<<LED_1);
- PORTB->PCR[LED_2]|=PORT_PCR_MUX(1);
- PTB->PDDR|=(1<<LED_2);
- PORTB->PCR[LED_3]|=PORT_PCR_MUX(1);
- PTB->PDDR|=(1<<LED_3);
- PORTB->PCR[LED_4]|=PORT_PCR_MUX(1);
- PTB->PDDR|=(1<<LED_4);
- //Configure Port D pin 0 as GPIO, Input, Pull-up resistor
- PORTD->PCR[SW_1]|=PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(0);
- PTD->PDDR&=~(1<<SW_1);
- PORTD->PCR[SW_2]|=PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(0);
- PTD->PDDR&=~(1<<SW_2);
- PORTD->PCR[SW_3]|=PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(0);
- PTD->PDDR&=~(1<<SW_3);
- PORTD->PCR[SW_4]|=PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(0);
- PTD->PDDR&=~(1<<SW_4);
- while(1){
- if( (PTD->PDIR & (1<<SW_1))==0){
- dMs(15);
- if(PTB->PDOR & (1<<LED_1))
- PTB->PDOR&=~(1<<LED_1);
- else
- PTB->PDOR|=(1<<LED_1);
- while((PTD->PDIR & (1<<SW_1))==0); //Wait for SW_1 release
- }
- if( (PTD->PDIR & (1<<SW_2))==0){
- dMs(15);
- if(PTB->PDOR & (1<<LED_2))
- PTB->PDOR&=~(1<<LED_2);
- else
- PTB->PDOR|=(1<<LED_2);
- while((PTD->PDIR & (1<<SW_2))==0); //Wait for SW_2 release
- }
- if( (PTD->PDIR & (1<<SW_3))==0){
- dMs(15);
- if(PTB->PDOR & (1<<LED_3))
- PTB->PDOR&=~(1<<LED_3);
- else
- PTB->PDOR|=(1<<LED_3);
- while((PTD->PDIR & (1<<SW_3))==0); //Wait for SW_3 release
- }
- if( (PTD->PDIR & (1<<SW_4))==0){
- dMs(15);
- if(PTB->PDOR & (1<<LED_4))
- PTB->PDOR&=~(1<<LED_4);
- else
- PTB->PDOR|=(1<<LED_4);
- while((PTD->PDIR & (1<<SW_4))==0); //Wait for SW_4 release
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement