Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //======================================================= //
- // Author: Ariel Antonowicz, Arkadiusz Kwiatkowski = //
- // Last modified: 08.12.2016 = //
- // RGB + BUTTONS (INTERUPTS LABORATORY) = //
- // Tiva C Series TM4C123G = //
- // Doc: Tiva - DATA SHEET = //
- // Tiva - TM4C123G = //
- // Lab II - Example (BUTOONS INTERUPS) = //
- //======================================================= //
- #include <stdint.h>
- #include <tm4c123gh6pm.h>
- #define FIRST_INTERRUPT_NUMBER 16
- int isLightsChanging = 0;
- int isBlueBlinks = 0;
- int isSw1 = 0;
- int isSw2 = 0;
- int timeCounter = 0;
- typedef enum EColor {
- GREEN = 0,
- BLUE,
- RED
- };
- static void init(){
- SYSCTL_RCGCGPIO_R = 0x20;
- GPIO_PORTF_LOCK_R = 0x4C4F434B;
- GPIO_PORTF_CR_R = 0xff;
- GPIO_PORTF_DIR_R = 0xe;
- GPIO_PORTF_PUR_R = 0x11;
- GPIO_PORTF_DEN_R = 0x1f;
- GPIO_PORTF_IS_R = 0x0;
- GPIO_PORTF_IBE_R = 0x0;
- GPIO_PORTF_IEV_R = 0x0;
- GPIO_PORTF_IM_R = 0x11;
- // wlacz przerwania dla przyciskow
- NVIC_EN0_R |= (1<<(INT_GPIOF-FIRST_INTERRUPT_NUMBER));
- SYSCTL_RCGCTIMER_R |= (1<<0);
- // wylacz timer aby moc go skonfigurowac
- TIMER0_CTL_R &= ~(1<<0);
- TIMER0_CFG_R = 0x0;
- //0x2 periodic mode(liczy w kolko), 0x1 one-shot mode
- TIMER0_TAMR_R |= (0x2<<0);
- // liczy w dol
- TIMER0_TAMR_R &= ~(1<<4);
- TIMER0_TAILR_R = 0x00F42400; // 16 milionow, bo 16MHz zegar jest, czyli 16 milionow cykli da 1 sekunde
- // jak doliczy do zera, zostanie wywolane przerwanie
- TIMER0_IMR_R |= (1<<0);
- //wgranie przerwan z pliku startup, pierwszych 16 sie nie modyfikuje przerwan
- NVIC_EN0_R |= (1<<(INT_TIMER0A-FIRST_INTERRUPT_NUMBER));
- // wlacz timer ( wartosc 1)
- TIMER0_CTL_R |= (1<<0);
- // aktywacja UART
- SYSCTL_RCGCUART_R |= (1<<0);
- // ustawianie portu A w trybie run mode
- SYSCTL_RCGCGPIO_R |= (1<<0);
- // do portu a przypisujemy uart (PA0 i PA1 nie sa traktowane jako GPIO tylko jako UART)
- GPIO_PORTA_AFSEL_R = (1<<1) | (1<<0);
- GPIO_PORTA_PCTL_R = (1<<0) | (1<<4);
- GPIO_PORTA_DEN_R = (1<<0) | (1<<1);
- // dezaktywacja UART w celu konfiguracji
- UART0_CTL_R &= ~(1<<0);
- // dzielnik szybkosci
- UART0_IBRD_R = 104;
- // dzielnik transmisji
- UART0_FBRD_R = 11;
- // dlugosc slowa, bit parzystosci, bit stopu (8 bit, bez bitu parzystosci 1-stop bit)
- UART0_LCRH_R = (0x3<<5);
- // wlaczamy uart
- UART0_CTL_R = (1<<0) | (1<<8) | (1<<9);
- }
- static void setColor(enum EColor color){
- unsigned char portData;
- switch (color){
- case GREEN:
- portData = (1<<3);
- break;
- case BLUE:
- portData = (1<<2);
- break;
- case RED:
- portData = (1<<1);
- break;
- }
- GPIO_PORTF_DATA_R &= 0xF1;
- GPIO_PORTF_DATA_R |= portData;
- }
- void GPIOF_Handler(void)
- { // w rekacji na nacisniecie przycisku zaswiec dioda
- if(isLightsChanging == 0) {
- if (GPIO_PORTF_DATA_R & 0x10){ //1010
- isSw1 = 1;
- } else {
- if(isSw2) {
- isSw2 = 0;
- setColor(GREEN);
- }
- else
- isSw2 = 1;
- }
- }
- // przerwanie zostalo obsluzone
- GPIO_PORTF_ICR_R = 0x11;
- }
- //Oczekuje na wcisniecie przycisku z klawiatury, a wcisniety klawisz zapisuje do c
- char readChar(void)
- {
- char c;
- while((UART0_FR_R & (1<<4)) != 0);
- c = UART0_DR_R;
- return c;
- }
- //Pobiera znak i wyswietla na konsoli
- void printChar(char c)
- {
- while((UART0_FR_R & (1<<5)) != 0);
- UART0_DR_R = c;
- }
- //Wyswietla bajt po bajcie slowo w konsoli
- void printString(char * string)
- {
- while(*string)
- {
- printChar(*(string++));
- }
- }
- void TIMER0A_Handler(void)
- {
- if(isSw1 == 1) {
- isLightsChanging = 1;
- timeCounter++;
- setColor(GREEN);
- if(timeCounter == 5) {
- setColor(BLUE);
- TIMER0_ICR_R |= (1<<0);
- return;
- }
- if(timeCounter == 6)
- {
- setColor(RED);
- TIMER0_ICR_R |= (1<<0);
- return;
- }
- if(timeCounter == 11)
- {
- setColor(BLUE);
- TIMER0_ICR_R |= (1<<0);
- return;
- }
- if(timeCounter == 13) {
- setColor(GREEN);
- isSw1 = 0;
- isLightsChanging = 0;
- timeCounter == 0;
- return;
- }
- }
- if(isSw2 == 1) {
- unsigned char portData;
- if(isBlueBlinks == 1) {
- isBlueBlinks = 0;
- portData = (0<<2);
- GPIO_PORTF_DATA_R &= 0xF1;
- GPIO_PORTF_DATA_R |= portData;
- }
- else {
- isBlueBlinks = 1;
- setColor(BLUE);
- }
- }
- TIMER0_ICR_R |= (1<<0); // przerwanie zostalo obsluzone, wraca do main
- }
- int main()
- {
- char c;
- init();
- setColor(GREEN);
- // brak dostepu do neta na zaliczeniu!
- // wszystko na pendrive!
- // na pol oceny wyzej:
- //
- // R B G
- // |---------1s------------------------|
- // | |
- // --> G --> SW1 -5s-> B -1s-> E -5s-> B->|
- // | |
- // | |-> przycisk s -> B-blink -> Sw2---|
- // | |
- // |---------------------------------------
- while(1){
- printString("Enter \"s\"");
- c = readChar();
- printChar(c);
- printString("\n\r");
- switch(c)
- {
- case 's':
- isSw2 = 1;
- break;
- default:
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement