Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include "tm4c123gh6pm.h"
- #include "string.h"
- #define SYSTICK_RELOAD_VALUE 80000 // 5 mS
- #define NVIC_INT_CTRL_PEND_SYST 0x04000000 // Pend a systick int
- #define NVIC_INT_CTRL_UNPEND_SYST 0x02000000 // Unpend a systick int
- #define SYSTICK_PRIORITY 0x7E
- /*
- 0x02 - red
- 0x04 - blue
- 0x06 - magenta (rozowy)
- 0x08 - green
- 0x0A - yellow
- 0x0C - cyan
- 0x0E - white
- */
- volatile static int tim, zd;
- //for couting the interval between button clicks
- static int a;
- //a - iteration of timer, one is 5 ms
- static int aa,or,sw,n,b,xd,i;
- //aa - auto; or - reverse; sw - normal; n - clicks;
- //b - needed for clicking; xd - not used; i - for iteration
- static int vibr; //for the button vibration
- void SwitchM(void)
- {
- switch (i)
- {
- case 0:
- GPIO_PORTF_DATA_R=0x00;
- i=7;
- break;
- case 1:
- GPIO_PORTF_DATA_R=0x08;
- i--;
- break;
- case 2:
- GPIO_PORTF_DATA_R=0x04;
- i--;
- break;
- case 3:
- GPIO_PORTF_DATA_R=0x0C;
- i--;
- break;
- case 4:
- GPIO_PORTF_DATA_R=0x02;
- i--;
- break;
- case 5:
- GPIO_PORTF_DATA_R=0x0A;
- i--;
- break;
- case 6:
- GPIO_PORTF_DATA_R=0x06;
- i--;
- break;
- case 7:
- GPIO_PORTF_DATA_R=0x0E;
- i--;
- break;
- default:
- ;
- }
- }
- void SwitchP(void)
- {
- switch (i)
- {
- case 0:
- GPIO_PORTF_DATA_R=0x00;
- i++;
- break;
- case 1:
- GPIO_PORTF_DATA_R=0x08;
- i++;
- break;
- case 2:
- GPIO_PORTF_DATA_R=0x04;
- i++;
- break;
- case 3:
- GPIO_PORTF_DATA_R=0x0C;
- i++;
- break;
- case 4:
- GPIO_PORTF_DATA_R=0x02;
- i++;
- break;
- case 5:
- GPIO_PORTF_DATA_R=0x0A;
- i++;
- break;
- case 6:
- GPIO_PORTF_DATA_R=0x06;
- i++;
- break;
- case 7:
- GPIO_PORTF_DATA_R=0x0E;
- i=0;
- break;
- default:
- ;
- }
- }
- void SysTick_Handler(void) //interrupt for systick
- {
- a++; //iteration
- if(zd==1)
- {
- tim++;
- }
- if(a>20 && zd==0)vibr=1; //vibration
- if(a>=400 && zd==0) //auto mode
- {
- aa=1;
- sw=0;
- n=0;
- a=0;
- }
- if(aa==1 && a>50 && or==0) //auto mode next, switching lights
- {
- //if (i!=0 && i!=7)i++;
- SwitchP();
- if(n>=1)
- {
- aa=0;
- a=0;
- }
- a=0;
- }
- if(aa==1 && a>50 && or==1) //auto mode next, switching lights
- {
- SwitchM();
- if(n>=1)
- {
- aa=0;
- a=0;
- }
- a=0;
- }
- if(n>2)n=0;
- }
- void enable_global_int() //enabling global intreuprts
- {
- // enable interrupts.
- __asm("cpsie i");
- }
- void disable_global_int() //for disabling, but not used
- {
- // disable interrupts.
- __asm("cpsid i");
- }
- void SysTickInit() //initlaizioatn of systick
- {
- // Disable systick timer
- NVIC_ST_CTRL_R &= ~(NVIC_ST_CTRL_ENABLE);
- // Set current systick counter to reload value
- NVIC_ST_CURRENT_R = SYSTICK_RELOAD_VALUE;
- // Set Reload value, Systick reload register
- NVIC_ST_RELOAD_R = SYSTICK_RELOAD_VALUE;
- // NVIC systick setup, vector number 15
- // Clear pending systick interrupt request
- NVIC_INT_CTRL_R |= NVIC_INT_CTRL_UNPEND_SYST;
- // Set systick priority to 0x10, first clear then set.
- NVIC_SYS_PRI3_R &= ~(NVIC_SYS_PRI3_TICK_M);
- NVIC_SYS_PRI3_R |= (NVIC_SYS_PRI3_TICK_M & (SYSTICK_PRIORITY<<NVIC_SYS_PRI3_TICK_S));
- // Select systick clock source, Use core clock
- NVIC_ST_CTRL_R |= NVIC_ST_CTRL_CLK_SRC;
- // Enable systick interrupt
- NVIC_ST_CTRL_R |= NVIC_ST_CTRL_INTEN;
- // Enable and start timer
- NVIC_ST_CTRL_R |= NVIC_ST_CTRL_ENABLE;
- }
- void Reset() //resetting systick
- {
- // Disable systick timer
- NVIC_ST_CTRL_R &= ~(NVIC_ST_CTRL_ENABLE);
- // Set current systick counter to reload value
- NVIC_ST_CURRENT_R = SYSTICK_RELOAD_VALUE;
- NVIC_ST_RELOAD_R = SYSTICK_RELOAD_VALUE;
- NVIC_INT_CTRL_R |= NVIC_INT_CTRL_UNPEND_SYST;
- NVIC_ST_CTRL_R |= NVIC_ST_CTRL_ENABLE;
- }
- void Off() //turning off systick
- {
- // Disable systick timer
- NVIC_ST_CTRL_R &= ~(NVIC_ST_CTRL_ENABLE);
- }
- int main(void)
- {
- enable_global_int(); //enabling functions, and stuff
- int dummy;
- SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOF;
- dummy = SYSCTL_RCGC2_R;
- GPIO_PORTF_DIR_R=0x0E; //DDRA
- GPIO_PORTF_DEN_R=0x1E; //like PORTA
- GPIO_PORTF_LOCK_R = 0x4C4F434B; // Unlock the CR register
- GPIO_PORTF_CR_R = 0xFF; // Enable overwrite of PUR to FP0
- GPIO_PORTF_PUR_R=0x10; //PULL UP HERE
- //setting some variables at 0, initial values
- aa=or=sw=n=b=xd=i=a=tim=xd=0;
- //setting iintial values for data_r, just in case
- GPIO_PORTF_DATA_R=0x00;
- // let the normal mode be at the begining
- sw=1;
- //initalization of systick, then turning off
- SysTickInit();
- Off();
- //vibartion
- vibr=1;
- while(1)
- {
- if (GPIO_PORTF_DATA_R & 0x10) //not pushing!
- {
- if(aa==0 && zd==0) //if there is no automode, it doesnt count to anything
- {
- Off();
- a=0;
- }
- if(zd==0 && aa==0 && n==1 && b==1)
- {
- Off();
- Reset();
- zd=1;
- }
- //activating switfh
- if(aa==0)sw=1;
- //activing order reverse
- if(xd==1 && or==1)
- {
- or=0;
- xd=0;
- }
- if(xd==1 && or==0)
- {
- or=1;
- xd=0;
- }
- //switch in work
- if (sw==1 && b==1 && or==0 && vibr ==1)
- {
- SwitchP();
- }
- //reverse in action
- if (sw==1 && or==1 && b==1 && vibr==1)
- {
- SwitchM();
- //if (i!=0 && i!=7)i--;
- }
- b=0;
- }
- else //pushing!
- {
- if(b==0)
- {
- n++;
- if(n==2 && tim<100 && zd==1) //500 ms
- {
- xd=1;
- zd=n=a=tim=0;
- }
- else if(n==2 && zd==1)
- {
- zd=n=a=tim=0;
- }
- b=1;
- Reset();
- }
- vibr=0;
- while(vibr==0); //so the vibration would not have effect, it waits
- }
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement