commit 000f1cf5c50e8c78dc70e68a8afba2d00b31e24d Author: aj Date: Tue Dec 3 15:18:11 2019 +0000 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fe9336e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.uvoptx +*.uvprojx +*.uvguix.andy_ +*.scvd diff --git a/7Seg Practice/7Seg.c b/7Seg Practice/7Seg.c new file mode 100755 index 0000000..09d592c --- /dev/null +++ b/7Seg Practice/7Seg.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +void DelayMs(int s); +void initGPIO(void); + +int main(void) +{ + initGPIO(); + + int numbers[16] = { + 0x3F, + 0x06, + 0x5B, + 0x4F, + 0x66, + 0x6D, + 0x7D, + 0x07, + 0x7F, + 0x6F, + 0x77, + 0x7C, + 0x39, + 0x5E, + 0x79, + 0x71 + }; + + int a = 0; + GPIOA -> DATA = (numbers[a] << 2) & 0x1C; + GPIOC -> DATA = (numbers[a] << 1) & 0xF0; + + bool currSW2state; + bool prevSW2state = 0; + + for(;;){ + + currSW2state = !(GPIOF -> DATA & 0x01); + + if(currSW2state && !prevSW2state){ + a++; + if(a == 16) a = 0; + GPIOA -> DATA = (numbers[a] << 2) & 0x1C; + GPIOC -> DATA = (numbers[a] << 1) & 0xF0; + } + + if(!(GPIOF -> DATA & 0x10)){ + a = 0; + GPIOA -> DATA = (numbers[a] << 2) & 0x1C; + GPIOC -> DATA = (numbers[a] << 1) & 0xF0; + } + + prevSW2state = currSW2state; + + } +} + +void initGPIO(){ + // GPIO Configuration + // 1. Enable Clock on Port F (SYSCTL_RCGCGPIO) + SYSCTL->RCGCGPIO = 0x2D; + // allow time for clock to stabilize (SYSCTL_PRGPIO) + while((SYSCTL->PRGPIO & 0x2D) != 0x2D){}; + // 2. Unlock GIOD[7] and GPIOF[0] on TM4C123G (GPIOn->LOCK and GPIOn->CR) + GPIOF->LOCK = 0x4C4F434B; + GPIOD->LOCK = 0x4C4F434B; + GPIOF->CR |= 0x01; + GPIOD->CR |= 0x80; + // 3. Clear AMSEL to disable analog + //GPIOF->AMSEL = 0x00; + //GPIOC->AMSEL = 0x00; + //GPIOA->AMSEL = 0x00; + //GPIOD->AMSEL = 0x00; + // 4. Config PCTL to select GPIO + //GPIOF->PCTL = 0x00; + //GPIOA->PCTL = 0x00; + //GPIOC->PCTL = 0x00; + //GPIOD->PCTL = 0x00; + // 5. Set DIR to 0 for input, 1 for output + GPIOA->DIR |= 0x1C; + GPIOC->DIR |= 0xF0; + GPIOD->DIR |= 0x80; + GPIOF->DIR = 0x0E; // PF3,PF2,PF1 for Output + // 6. Clear AFSEL bits to 0 to select regular I/O + //GPIOF->AFSEL = 0x00; + //GPIOA->AFSEL = 0x00; + //GPIOC->AFSEL = 0x00; + //GPIOD->AFSEL = 0x00; + // 7. Set PUR bits to 1 to enable internal pull-up resistor + GPIOF->PUR = 0x11; + // 8. Set DEN bits to 1 to enable data pins + GPIOA->DEN = 0x1C; + GPIOC->DEN |= 0xF0; + GPIOD->DEN = 0x80; + GPIOF->DEN = 0x1F; // Enable all digital pins on PortF (PF3,PF2,PF1,PF0) +} diff --git a/FINAL/MyDefines.h b/FINAL/MyDefines.h new file mode 100755 index 0000000..146c609 --- /dev/null +++ b/FINAL/MyDefines.h @@ -0,0 +1,65 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#define _PWM_RIGHT_ALIG_CMPAD ((0x03 << 6) | (0x02 << 2)) +#define _PWM_RIGHT_ALIG_CMPBD ((0x03 << 10) | (0x02 << 2)) + +#define _TIMER0 0x01 +#define _TIMER1 0x02 +#define _TIMER2 0x04 +#define _TIMER3 0x08 +#define _TIMER4 0x10 +#define _TIMER5 0x20 +#define _TIMER6 0x40 +#define _TIMER7 0x80 + +#define _TIMERA_ENABLE (1 << 0) +#define _TIMERB_ENABLE (1 << 8) +#define _TIMERA_COUNTUP (1 << 4) +#define _TIMERA_COUNTDOWN (0 << 4) +#define _TIMERA_CAPTURE 0x03 +#define _TIMERA_EDGE_TIME (1 << 2) +#define _TIMERA_POSITIVE_EDGE 0 +#define _TIMERA_NEGATIVE_EDGE (1 << 2) +#define _TIMERA_BOTH_EDGES (3 << 2) + +#endif \ No newline at end of file diff --git a/FINAL/main.c b/FINAL/main.c new file mode 100755 index 0000000..c1db107 --- /dev/null +++ b/FINAL/main.c @@ -0,0 +1,112 @@ +#include +#include +#include +#include + +#include +//#include RCGCGPIO = _PORTC|_PORTD|_PORTF; + // allow time for clock to stabilize + while((SYSCTL->PRGPIO & (_PORTC|_PORTD|_PORTF)) != (_PORTC|_PORTD|_PORTF)){}; + + // 2. Unlock GPIO + GPIOF->LOCK = 0x4C4F434B; + GPIOF->CR |= 0x01; + // 3. Clear AMSEL to disable analog + // 4. Config PCTL to select GPIO + GPIOC->PCTL= 0x000000; + // 5. Set DIR to 0 for input, 1 for output + GPIOC -> DIR |= _PIN5; + GPIOD -> DIR = 0x0; + GPIOF -> DIR = _PIN0|_PIN1|_PIN3; + // 6. Enable AFSEL bits to 1 + GPIOC -> AFSEL |= _PIN5; + // 7. Set PUE bits to 1 to enable internal pull-up + GPIOD -> PDR= _PIN6; + GPIOF -> PUR= _PIN0; + // 8. Set DEN bits to 1 to enable data pins + GPIOF->DEN= _PIN1 | _PIN4 | _PIN2; +} +//------------------------------------------------------------------------------ diff --git a/FINALFINAL/MyDefines.h b/FINALFINAL/MyDefines.h new file mode 100755 index 0000000..146c609 --- /dev/null +++ b/FINALFINAL/MyDefines.h @@ -0,0 +1,65 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#define _PWM_RIGHT_ALIG_CMPAD ((0x03 << 6) | (0x02 << 2)) +#define _PWM_RIGHT_ALIG_CMPBD ((0x03 << 10) | (0x02 << 2)) + +#define _TIMER0 0x01 +#define _TIMER1 0x02 +#define _TIMER2 0x04 +#define _TIMER3 0x08 +#define _TIMER4 0x10 +#define _TIMER5 0x20 +#define _TIMER6 0x40 +#define _TIMER7 0x80 + +#define _TIMERA_ENABLE (1 << 0) +#define _TIMERB_ENABLE (1 << 8) +#define _TIMERA_COUNTUP (1 << 4) +#define _TIMERA_COUNTDOWN (0 << 4) +#define _TIMERA_CAPTURE 0x03 +#define _TIMERA_EDGE_TIME (1 << 2) +#define _TIMERA_POSITIVE_EDGE 0 +#define _TIMERA_NEGATIVE_EDGE (1 << 2) +#define _TIMERA_BOTH_EDGES (3 << 2) + +#endif \ No newline at end of file diff --git a/FINALFINAL/main.c b/FINALFINAL/main.c new file mode 100755 index 0000000..e427161 --- /dev/null +++ b/FINALFINAL/main.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include + +#include +//#include _3_CMPB = cmp_1_0m; + if(SW1) state = S_0; + if(SW2) state = S_IDLE; + + *pLed1 = 0x0; + *pLed2 = 0x0; + + break; + case S_0: + PWM0->_3_CMPB = cmp_1_0m; + if(SW1) state = S_1; + if(SW2) state = S_2; + *pLed2 = 0x0; + + if(i%2 == 0){ //200ms delay + //blink led1 + if(!(*pLed1 & _PIN1)) *pLed1 = 0xFF; + else *pLed1 = 0x00; + } + + break; + case S_1: + PWM0->_3_CMPB = cmp_1_5m; + if(SW1) state = S_2; + if(SW2) state = S_IDLE; + + if(i%2 == 0){ //200ms delay + //blink led1 and led2 + if(!(*pLed1 & _PIN1)) *pLed1 = 0xFF; + else *pLed1 = 0x00; + if(!(*pLed2 & _PIN3)) *pLed2 = 0xFF; + else *pLed2 = 0x00; + } + + break; + case S_2: + PWM0->_3_CMPB = cmp_2_0m; + if(SW1) state = S_IDLE; + if(SW2) state = S_0; + *pLed1 = 0x0; + + if(i%2 == 0){ //200ms delay + //blink led2 + if(!(*pLed2 & _PIN3)) *pLed2 = 0xFF; + else *pLed2 = 0x00; + } + + break; + } + i++; + timer_waitMillis(100); // This is the only delay function in the code. + } +} +//------------------------------------------------------------------------------ +void Setup_PWM(void) +{ + // 1. Enable Clock for PWM Module 1 + SYSCTL->RCGCPWM |= _PWM_MODULE0; + while((SYSCTL->PRPWM & _PWM_MODULE0)==0){}; + // 2. Enable and Setup Clock Divider for PWM + SYSCTL->RCC |= (1 << 20); // RCC[20]=1:USEPWMDIV + SYSCTL->RCC &= ~0x000E0000; // RCC[19:17]=000 PWMDIV + SYSCTL->RCC |= (_PWMDIV_32 << 17); // RCC[19:17]=0x04 divider=/32 + // 3. Disable PWM Generator 2 + PWM0->_3_CTL &= ~0x01; // Disable PWM Generator 2 + // 4. Config LOAD, CMPn, GENn values + PWM0->_3_LOAD = 25000; // GEN 3 B PWM 7 PF3 MOTOR + PWM0->_3_CMPB = 23750; + PWM0->_3_GENB = _PWM_LEFT_ALIG_CMPBD;//0x080C + // 5. Enable PWM Generator 3 + PWM0->_3_CTL |= 0x01; + // 6. Enable PWM7 Output + PWM0 -> ENABLE |= _PWM7; +} +//------------------------------------------------------------------------------ +void Setup_GPIO(void) +{ + // Config for GPIO + // 1. Enable Clock on GPIOF + SYSCTL->RCGCGPIO = _PORTC|_PORTD|_PORTF; + // allow time for clock to stabilize + while((SYSCTL->PRGPIO & (_PORTC|_PORTD|_PORTF)) != (_PORTC|_PORTD|_PORTF)){}; + // 2. Unlock GPIO + GPIOF->LOCK = 0x4C4F434B; + GPIOF->CR |= 0x01; + // 3. Clear AMSEL to disable analog + // 4. Config PCTL to select GPIO + GPIOC->PCTL |= 0x400000; //M0PWM7 GEN3 + // 5. Set DIR to 0 for input, 1 for output + GPIOC -> DIR |= _PIN5; + GPIOD -> DIR = 0x0; + GPIOF -> DIR |= _PIN1|_PIN3; + // 6. Enable AFSEL bits to 1 + GPIOC -> AFSEL |= _PIN5; + GPIOD -> AFSEL = 0x0; + GPIOF -> AFSEL = 0x0; + // 7. Set PUE bits to 1 to enable internal pull-up + GPIOD -> PDR |= _PIN6; + GPIOF -> PUR |= _PIN0; + // 8. Set DEN bits to 1 to enable data pins + GPIOC->DEN |= _PIN5; + GPIOD->DEN |= _PIN6; + GPIOF->DEN |= _PIN0 | _PIN1 | _PIN3; +} +//------------------------------------------------------------------------------ diff --git a/HelloWorld/apples.c b/HelloWorld/apples.c new file mode 100755 index 0000000..bd5dd73 --- /dev/null +++ b/HelloWorld/apples.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +void DelayMs(int s); +void Delay(int t); + +int main(void) +{ + + // GPIO Configuration + // 1. Enable Clock on GPIOF[3:1] (SYSCTL_RCGCGPIO) + SYSCTL->RCGCGPIO = 0x20; + // allow time for clock to stabilize (SYSCTL_PRGPIO) + while((SYSCTL->PRGPIO & 0x20) != 0x20){}; + // 2. Unlock GPIOC[3:0], GIOD[7] and GPIOF[0] on TM4C123G (GPIOn->LOCK and GPIOn->CR) NOT USING + // 3. Clear AMSEL to disable analog + GPIOF->AMSEL = 0x00; + // 4. Config PCTL to select GPIO + GPIOF->PCTL = 0x0000; + //or + //GPIOF->PCTL &= 0xFFF00FFF; + // 5. Set DIR to 0 for input, 1 for output + GPIOF->DIR = 0x08; // PF3,PF2,PF1 for Output + // 6. Enable AFSEL bits to 1 + GPIOF->AFSEL = 0x00; + // 7. Set PUR bits to 1 to enable internal pull-up + GPIOF->PUR = 0x10; + // 8. Set DEN bits to 1 to enable data pins + GPIOF->DEN = 0x0E; // Enable digital pin on PF3,PF2,PF1 + + int sample = GPIOF -> DATA; + sample |= 0x0E; + //sample |= 0x08; + GPIOF->DATA = sample; + +} \ No newline at end of file diff --git a/ISR/MyDefines.h b/ISR/MyDefines.h new file mode 100755 index 0000000..fb99053 --- /dev/null +++ b/ISR/MyDefines.h @@ -0,0 +1,43 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#endif \ No newline at end of file diff --git a/ISR/main.c b/ISR/main.c new file mode 100755 index 0000000..b71bc35 --- /dev/null +++ b/ISR/main.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include "MyDefines.h" + +#include "TM4C123GH6PM.h" + +void SetupGPIO(void); +void SetupGPIOIRQ(void); +void GPIOF_Handler(void); + +int main() +{ + SetupGPIO(); + SetupGPIOIRQ(); + + while(1){ + + } +} + +void SetupGPIO(void){ + + // Config for GPIO + // 1. Enable Clock on GPIOF + SYSCTL->RCGCGPIO = _PORTF; + // allow time for clock to stabilize + while((SYSCTL->PRGPIO & _PORTF) != _PORTF){}; + // 2. Unlock GPIO + // 3. Clear AMSEL to disable analog + GPIOF -> AMSEL = 0x0; + // 4. Config PCTL to select GPIO + GPIOF -> PCTL = 0x0; + // 5. Set DIR to 0 for input, 1 for output + GPIOF -> DIR = _PIN1; + // 6. Enable AFSEL bits to 1 + GPIOF -> AFSEL = 0x0; + // 7. Set PUE bits to 1 to enable internal pull-up + GPIOF -> PUR = _PIN4; + // 8. Set DEN bits to 1 to enable data pins + GPIOF -> DEN = _PIN4|_PIN1; +} + +void SetupGPIOIRQ() +{ + uint32_t n, r, m, r2; + // 1. Disable IRQ in GPIOIM register + GPIOF->IM &= ~(_PIN4); + // 2. Configure IRQ Type (0=edge, 1=level) in the GPIOIS register + GPIOF->IS &= ~(_PIN4); + // 3. Configure GPIOIBE (0=single edge, 1=both edge), + // GPIOIEV (0=low level or falling edge, 1=high level or rising edge) + GPIOF->IBE &= ~(_PIN4); + GPIOF->IEV &= ~(_PIN4); + // 4. Clear the GPIORIS register + GPIOF->ICR |= _PIN4; + // 5. Enable IRQ in the GPIOIM register + GPIOF->IM |= _PIN4; + // 6. Set priority in the NVIC + n = 30 / 4; + r = 21; // remainder:0 -> r = 5, :1-> r=13, :2-> r=21, :3-> r=29 + m = 30 / 32; r2 = 30 % 32; + NVIC->IP[n] |= (3 << r); + // 7. Enable IRQ in the NVIC + NVIC->ISER[m] |= (1 << r2); +} + +uint32_t* pn1 = (uint32_t*)(((char*)GPIOF) + (_PIN1 << 2)); +void GPIOF_Handler(){ + GPIOF->ICR |= _PIN4; //clear interrupt flag + *pn1 ^= _PIN1; + +} \ No newline at end of file diff --git a/KeyPadSave.txt b/KeyPadSave.txt new file mode 100755 index 0000000..4b3f979 --- /dev/null +++ b/KeyPadSave.txt @@ -0,0 +1,188 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +#include "ez123G.h" +#include "MyDefines.h" + +char str[100]; +char ReadKeyPad2(); +char ReadKeyPad1(); +char ReadKeyPad(); + +int main(void) +{ + PEZOBJ_LCD lcd; + uint16_t i = 0; + + // GPIO Initialization and Configuration + // 1. Enable Clock on GPIOs + SYSCTL->RCGCGPIO |= 0x1B; + // allow time for clock to stabilize + while ((SYSCTL->PRGPIO & 0x1B) != 0x1B) {}; + // 2. Unlock PD7 and/or PF0 for TM4C 123G + // 3. Config AMSEL to disable analog function + // 4. Config PCTL to select 0-GPIO + // 5. Set AFSEL bits to 0 + // 6. Set DIR to 0 for input, 1 for output + + GPIOA -> DIR = 0x00; + GPIOB -> DIR = 0x03; + GPIOD -> DIR = 0x0F; + GPIOE -> DIR = 0x3E; + + // 7. Set PUR/PDR/ODR bits to 1 to enable internal pull-up/-down resistir and/or open-drain + GPIOB -> ODR = 0x03; + GPIOE -> ODR = 0x30; + GPIOA -> PUR = 0xE0; + GPIOB -> PUR = 0x10; + + // 8. Set DEN bits to 1 to enable all + GPIOA -> DEN = 0xE0; + GPIOB -> DEN = 0x13; + GPIOD -> DEN = 0x0F; + GPIOE -> DEN = 0x3E; + + + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIOE, PIN1); + ezLCD_Connect_RSPin(lcd, GPIOE, PIN2); + ezLCD_Connect_RWPin(lcd, GPIOE, PIN3); + + ezLCD_Start(lcd); + ezLCD_ClearDisplay(lcd); + + char ch; + while(1){ + ch = ReadKeyPad(); + + if (ch == '*'){ + i *= 100; + ezLCD_ClearDisplay(lcd); + } + if (ch == '#') i = 0; + if (ch >='0' && ch <= '9') i = i*10 + ch - '0'; + + sprintf(str, "%c ", ch); + ezLCD_Position(lcd, 0, 0); + ezLCD_PrintString(lcd, str); + + timer_waitMillis(100); + } +} +//-------------------------------------------------------------- +char KeyPad[4][4]={ + {'1','2','3','A'}, + {'4','5','6','B'}, + {'7','8','9','C'}, + {'*','0','#','D'}}; + +char ReadKeyPad1() +{ + + GPIOB -> DATA |= 0x03; + GPIOE -> DATA |= 0x30; + //GPIOB -> DATA |= 0x01; + GPIOB -> DATA &= ~0x02; + //GPIOE -> DATA |= 0x10; + //GPIOE -> DATA &= ~0x20; + + timer_waitMillis(10); + + if(!(GPIOB -> DATA & 0x10)) return KeyPad[1][0]; + if(!(GPIOA -> DATA & 0x20)) return KeyPad[1][1]; + if(!(GPIOA -> DATA & 0x40)) return KeyPad[1][2]; + if(!(GPIOA -> DATA & 0x80)) return KeyPad[1][3]; + + return 0x0; +} + +char ReadKeyPad2() +{ + int row; + GPIOB -> DATA |= 0x03; + GPIOE -> DATA |= 0x30; + for(row = 0; row < 4; row++){ + switch(row){ + case 0: + GPIOB -> DATA &= ~0x01; + timer_waitMillis(10); + if(!(GPIOB -> DATA & 0x10)) return KeyPad[0][0]; + if(!(GPIOA -> DATA & 0x20)) return KeyPad[0][1]; + if(!(GPIOA -> DATA & 0x40)) return KeyPad[0][2]; + if(!(GPIOA -> DATA & 0x80)) return KeyPad[0][3]; + break; + case 1: + GPIOB -> DATA |= 0x01; + GPIOB -> DATA &= ~0x02; + timer_waitMillis(10); + if(!(GPIOB -> DATA & 0x10)) return KeyPad[1][0]; + if(!(GPIOA -> DATA & 0x20)) return KeyPad[1][1]; + if(!(GPIOA -> DATA & 0x40)) return KeyPad[1][2]; + if(!(GPIOA -> DATA & 0x80)) return KeyPad[1][3]; + break; + case 2: + GPIOB -> DATA |= 0x02; + GPIOE -> DATA &= ~0x10; + timer_waitMillis(10); + if(!(GPIOB -> DATA & 0x10)) return KeyPad[2][0]; + if(!(GPIOA -> DATA & 0x20)) return KeyPad[2][1]; + if(!(GPIOA -> DATA & 0x40)) return KeyPad[2][2]; + if(!(GPIOA -> DATA & 0x80)) return KeyPad[2][3]; + break; + case 3: + GPIOE -> DATA |= 0x10; + GPIOE -> DATA &= ~0x20; + timer_waitMillis(10); + if(!(GPIOB -> DATA & 0x10)) return KeyPad[3][0]; + if(!(GPIOA -> DATA & 0x20)) return KeyPad[3][1]; + if(!(GPIOA -> DATA & 0x40)) return KeyPad[3][2]; + if(!(GPIOA -> DATA & 0x80)) return KeyPad[3][3]; + break; + } + + row++; + } + + return 0x0; +} + +char ReadKeyPad() +{ + int row; + GPIOB -> DATA |= 0x03; + GPIOE -> DATA |= 0x30; + for(row = 0; row < 4; row++){ + switch(row){ + case 0: + GPIOB -> DATA &= ~0x01; + break; + case 1: + GPIOB -> DATA |= 0x01; + GPIOB -> DATA &= ~0x02; + break; + case 2: + GPIOB -> DATA |= 0x02; + GPIOE -> DATA &= ~0x10; + break; + case 3: + GPIOE -> DATA |= 0x10; + GPIOE -> DATA &= ~0x20; + break; + } + + timer_waitMillis(10); + + if(!(GPIOB -> DATA & 0x10)) return KeyPad[row][0]; + if(!(GPIOA -> DATA & 0x20)) return KeyPad[row][1]; + if(!(GPIOA -> DATA & 0x40)) return KeyPad[row][2]; + if(!(GPIOA -> DATA & 0x80)) return KeyPad[row][3]; + + row++; + } + + return 0x0; +} \ No newline at end of file diff --git a/LCD Display/LCDLab.c b/LCD Display/LCDLab.c new file mode 100755 index 0000000..f2c3699 --- /dev/null +++ b/LCD Display/LCDLab.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +#include "ez123G.h" + +char str[100]; + +int main(void) +{ + PEZOBJ_LCD lcd; + uint16_t i = 0; + int array[]= {10, 35, 65, 43, 32, 86, 44, 9, 18}; + + // Enable GPIOD[3:0] and GPIOE[3:1] + // Config for GPIOD and GPIOE + // 1. Enable Clock on GPIOs + SYSCTL->RCGCGPIO |= 0x18; + // allow time for clock to stabilize + while ((SYSCTL->PRGPIO & 0x18) != 0x18) {}; + // 2. Unlock PD7 + GPIOD->LOCK = 0x4C4F434B; + GPIOD->CR |= 0x80; + + // 3. Config AMSEL to disable analog function + GPIOD -> AMSEL = 0x00; + GPIOE -> AMSEL = 0x00; + // 4. Config PCTL to select GPIO + GPIOD -> PCTL = 0x00; + GPIOE -> PCTL = 0x00; + // 5. Set DIR to 0 for input, 1 for output + GPIOD -> DIR |= 0x0F; + GPIOE -> DIR |= 0x0E; + + // 6. Set AFSEL bits to 0 + GPIOD -> AFSEL = 0x00; + GPIOE -> AFSEL = 0x00; + // 7. Set PUE bits to 1 to enable internal pull-up (Skipped) + // 8. Set DEN bits to 1 to enable data pins + GPIOD -> DEN |= 0x0F; + GPIOE -> DEN |= 0x0E; + + + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIOE, PIN1); + ezLCD_Connect_RSPin(lcd, GPIOE, PIN2); + ezLCD_Connect_RWPin(lcd, GPIOE, PIN3); + + ezLCD_Start(lcd); + ezLCD_LoadVerticalBargraphFonts(lcd); + + ezLCD_ClearDisplay(lcd); + + int j = 0; + float a; + int s = 0, M = array[0], m = array[0]; + int total = 0; + while(j <= 8){ + ezLCD_ClearDisplay(lcd); + s += array[j]; + if(array[j] > M) M = array[j]; + if(array[j] < m) m = array[j]; + a = s / (j + 1); + ezLCD_Position(lcd, 0, 0); + sprintf(str, "S=%d", s); + ezLCD_PrintString(lcd, str); + ezLCD_Position(lcd, 0, 8); + sprintf(str, "M=%d", M); + ezLCD_PrintString(lcd, str); + ezLCD_Position(lcd, 1, 0); + sprintf(str, "A=%.2f", a); + ezLCD_PrintString(lcd, str); + ezLCD_Position(lcd, 1, 8); + sprintf(str, "m=%d", m); + ezLCD_PrintString(lcd, str); + timer_waitMillis(500); + j++; + } +} \ No newline at end of file diff --git a/LEDButton/ledbutton.c b/LEDButton/ledbutton.c new file mode 100755 index 0000000..bb0d77d --- /dev/null +++ b/LEDButton/ledbutton.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +void initGPIO(void); +void Delay(int t); + +int main(void) +{ + initGPIO(); + int sample = 0x00; + + for(;;){ + + sample = GPIOF -> DATA; + + //SWITCH ONE ON + if(!(sample & 0x10)){ + GPIOF -> DATA |= 0x02; + Delay(2000); + GPIOF -> DATA &= 0xFD; + } + + //SWITCH TWO ON + if(!(sample & 0x01)){ + GPIOF -> DATA |= 0x08; + Delay(2000); + GPIOF -> DATA &= 0xF7; + } + + } +} + +void initGPIO(void){ + + // GPIO Configuration + // 1. Enable Clock on GPIOF[3:1] (SYSCTL_RCGCGPIO) + SYSCTL->RCGCGPIO = 0x20; + // allow time for clock to stabilize (SYSCTL_PRGPIO) + while((SYSCTL->PRGPIO & 0x20) != 0x20){}; + // 2. Unlock GPIOC[3:0], GIOD[7] and GPIOF[0] on TM4C123G (GPIOn->LOCK and GPIOn->CR) NOT USING + GPIOF->LOCK = 0x4C4F434B; + GPIOF->CR |= 0x01; + // 3. Clear AMSEL to disable analog + GPIOF->AMSEL = 0x00; + // 4. Config PCTL to select GPIO + GPIOF->PCTL = 0x0000; + //or + //GPIOF->PCTL &= 0xFFF00FFF; + // 5. Set DIR to 0 for input, 1 for output + GPIOF->DIR = 0x0E; // PF3,PF2,PF1 for Output + // 6. Enable AFSEL bits to 1 + GPIOF->AFSEL = 0x00; + // 7. Set PUR bits to 1 to enable internal pull-up + GPIOF->PUR = 0x11; + // 8. Set DEN bits to 1 to enable data pins + GPIOF->DEN = 0x1F; // Enable digital pin on PF3,PF2,PF1 +} + +void Delay(int t) +{ + volatile int i, j; + for (i = 0; i < t; i++) + for (j = 0; j < 3180; j++) + {}; +} diff --git a/LEDButton/ledfinal.txt b/LEDButton/ledfinal.txt new file mode 100755 index 0000000..e5cff86 --- /dev/null +++ b/LEDButton/ledfinal.txt @@ -0,0 +1,79 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +void DelayMs(int s); +void Delay(int t); + +int main(void) +{ + + // GPIO Configuration + // 1. Enable Clock on GPIOF[3:1] (SYSCTL_RCGCGPIO) + SYSCTL->RCGCGPIO = 0x20; + // allow time for clock to stabilize (SYSCTL_PRGPIO) + while((SYSCTL->PRGPIO & 0x20) != 0x20){}; + // 2. Unlock GPIOC[3:0], GIOD[7] and GPIOF[0] on TM4C123G (GPIOn->LOCK and GPIOn->CR) NOT USING + GPIOF->LOCK = 0x4C4F434B; + GPIOF->CR |= 0x01; + // 3. Clear AMSEL to disable analog + GPIOF->AMSEL = 0x00; + // 4. Config PCTL to select GPIO + GPIOF->PCTL = 0x0000; + //or + //GPIOF->PCTL &= 0xFFF00FFF; + // 5. Set DIR to 0 for input, 1 for output + GPIOF->DIR = 0x0E; // PF3,PF2,PF1 for Output + // 6. Enable AFSEL bits to 1 + GPIOF->AFSEL = 0x00; + // 7. Set PUR bits to 1 to enable internal pull-up + GPIOF->PUR = 0x11; + // 8. Set DEN bits to 1 to enable data pins + GPIOF->DEN = 0x1F; // Enable digital pin on PF3,PF2,PF1 + + + + int sample = 0x00; + int led = 0x00; + + for(;;){ + + sample = GPIOF -> DATA; + + //SWITCH ONE ON + if(!(sample & 0x10)){ + led = GPIOF -> DATA; + led |= 0x02; + GPIOF -> DATA = led; + Delay(60); + led = GPIOF -> DATA; + led &= 0xFD; + GPIOF -> DATA = led; + } + + + //SWITCH TWO ON + if(!(sample & 0x01)){ + led = GPIOF -> DATA; + led |= 0x08; + GPIOF -> DATA = led; + Delay(60); + led = GPIOF -> DATA; + led &= 0xF7; + GPIOF -> DATA = led; + } + } +} + +void Delay(int t) +{ + volatile long time; + int i; + for (i = 0; i < t; ++i){ + time = 145000; + while(time) + time--; + } +} \ No newline at end of file diff --git a/Linked List/MyDefines.h b/Linked List/MyDefines.h new file mode 100755 index 0000000..fb99053 --- /dev/null +++ b/Linked List/MyDefines.h @@ -0,0 +1,43 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#endif \ No newline at end of file diff --git a/Linked List/main.c b/Linked List/main.c new file mode 100755 index 0000000..4e45d5c --- /dev/null +++ b/Linked List/main.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include + +#include "MyDefines.h" +#include + +void Delay(int s) +{ + volatile int i, j; + for (i = 0; i < s; i++) + for (j = 0; j < 3180; j++){} +} + +void SetupGPIO(void); + +typedef struct STEPS{ + struct STEPS* prev; + uint8_t pinout1; + uint8_t pinout2; + struct STEPS* next; +} STEPS; + +#define CW 0 +#define CCW 1 +void Stepper(int s, bool direction); + +STEPS step[]={ + {&step[3], _PIN0, _PIN1, &step[1]}, + {&step[2], _PIN1, _PIN2, &step[2]}, + {&step[1], _PIN2, _PIN3, &step[3]}, + {&step[0], _PIN3, _PIN0, &step[0]}, +}; + +int main() +{ + int i = 0; + + SetupGPIO(); + + while(1){ + + for ( i = 0; i < 10; i++){ + Stepper(10, CW); + Delay(10); + } + /* + for (i = 0; i < 10; i++){ + Stepper(10, CCW); + Delay(10); + }*/ + } + +} + +void SetupGPIO(void) +{ + // GPIO Initialization and Configuration + // 1. Enable Clock on GPIOs + SYSCTL->RCGCGPIO |= _PORTD; + // allow time for clock to stabilize + while ((SYSCTL->PRGPIO & _PORTD) != _PORTD) {}; + // 2. Unlock PD7 and/or PF0 for TM4C 123G + // 3. Config AMSEL to disable analog function + // 4. Config PCTL to select 0-GPIO + // 5. Set AFSEL bits to 0 + // 6. Set DIR to 0 for input, 1 for output + GPIOD -> DIR = _PIN0|_PIN1|_PIN2|_PIN3; + // 7. Set PUR/PDR/ODR bits to 1 to enable internal pull-up/-down resistir and/or open-drain + // 8. Set DEN bits to 1 to enable all + GPIOD -> DEN = _PIN0|_PIN1|_PIN2|_PIN3; + +} +void Stepper(int s, bool direction) +{ + int i; + STEPS* p = step; + + for (i = 0; i< s; i++){ + if (direction == CW){ + // p pointed to next element of step array + GPIOD -> DATA |= ((p->pinout1)|(p->pinout2)); + Delay(10); + GPIOD -> DATA &= ~((p->pinout1)|(p->pinout2)); + p = p->next; + } else { + GPIOD -> DATA |= ((p->pinout1)|(p->pinout2)); + Delay(10); + GPIOD -> DATA &= ~((p->pinout1)|(p->pinout2)); + p = p->prev; + + } + // Port = p->pinout; + + } +} + \ No newline at end of file diff --git a/MyDefines.h b/MyDefines.h new file mode 100755 index 0000000..146c609 --- /dev/null +++ b/MyDefines.h @@ -0,0 +1,65 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#define _PWM_RIGHT_ALIG_CMPAD ((0x03 << 6) | (0x02 << 2)) +#define _PWM_RIGHT_ALIG_CMPBD ((0x03 << 10) | (0x02 << 2)) + +#define _TIMER0 0x01 +#define _TIMER1 0x02 +#define _TIMER2 0x04 +#define _TIMER3 0x08 +#define _TIMER4 0x10 +#define _TIMER5 0x20 +#define _TIMER6 0x40 +#define _TIMER7 0x80 + +#define _TIMERA_ENABLE (1 << 0) +#define _TIMERB_ENABLE (1 << 8) +#define _TIMERA_COUNTUP (1 << 4) +#define _TIMERA_COUNTDOWN (0 << 4) +#define _TIMERA_CAPTURE 0x03 +#define _TIMERA_EDGE_TIME (1 << 2) +#define _TIMERA_POSITIVE_EDGE 0 +#define _TIMERA_NEGATIVE_EDGE (1 << 2) +#define _TIMERA_BOTH_EDGES (3 << 2) + +#endif \ No newline at end of file diff --git a/PWM DC Motor/MyDefines.h b/PWM DC Motor/MyDefines.h new file mode 100755 index 0000000..fb99053 --- /dev/null +++ b/PWM DC Motor/MyDefines.h @@ -0,0 +1,43 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#endif \ No newline at end of file diff --git a/PWM DC Motor/MyDevice.h b/PWM DC Motor/MyDevice.h new file mode 100755 index 0000000..e69de29 diff --git a/PWM DC Motor/main.c b/PWM DC Motor/main.c new file mode 100755 index 0000000..01b5016 --- /dev/null +++ b/PWM DC Motor/main.c @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include "MyDefines.h" + +#include "TM4C123GH6PM.h" + +void SetupPWM(void); +void SetupGPIO(void); +void SetupPWMDEFINE(void); +void SetupGPIODEFINE(void); + +int main() +{ + SetupPWM(); + SetupGPIO(); + + int cmp = 0; + volatile int i; + + while(1){ + // Your Code Here + //GPIOF -> DATA = 0x01; + + PWM1 -> _3_CMPB = cmp; + PWM1 -> _2_CMPA = cmp++; + + if(cmp > 9999) cmp = 0; + + for(i = 0; i < 500; i++){} + } +} + +int mainDEFINE(){ + + uint32_t *sw1 = (uint32_t *)(((char*)GPIOF) + (_PIN4 << 2)); + uint32_t *led = (uint32_t *)(((char*)GPIOF) + (_PIN2 << 2)); + + SetupPWMDEFINE(); + SetupGPIODEFINE(); + + while(1){ + if(!(*sw1)){ + *led = 0xFF; + }else{ + *led = 0x00; + } + } +} + +void SetupGPIODEFINE(void){ + + // Config for GPIO + // 1. Enable Clock on GPIOF + SYSCTL->RCGCGPIO = _PORTF; + // allow time for clock to stabilize + while((SYSCTL->PRGPIO & _PORTF) != _PORTF){}; + + // 2. Unlock GPIO + GPIOF -> LOCK = 0x4C4F434B; + GPIOF -> CR |= 0x01; + // 3. Clear AMSEL to disable analog + GPIOF -> AMSEL = 0x0; + // 4. Config PCTL to select GPIO + GPIOF -> PCTL = 0x5000; + // 5. Set DIR to 0 for input, 1 for output + GPIOF -> DIR = _PIN2 | _PIN3; + // 6. Enable AFSEL bits to 1 + GPIOF -> AFSEL = _PIN3; + // 7. Set PUE bits to 1 to enable internal pull-up + GPIOF -> DATA = _PIN4; + // 8. Set DEN bits to 1 to enable data pins + GPIOF -> DEN = _PIN2 | _PIN3; + +} + +void SetupGPIO(void){ + + // Config for GPIO + // 1. Enable Clock on GPIOF + SYSCTL->RCGCGPIO = 0x20; + // allow time for clock to stabilize + while((SYSCTL->PRGPIO & 0x20) != 0x20){}; + + // 2. Unlock GPIO + GPIOF -> LOCK = 0x4C4F434B; + GPIOF -> CR |= 0x01; + // 3. Clear AMSEL to disable analog + GPIOF -> AMSEL = 0x0; + // 4. Config PCTL to select GPIO + GPIOF -> PCTL = 0x00005005; + // 5. Set DIR to 0 for input, 1 for output + GPIOF -> DIR = 0x09; + // 6. Enable AFSEL bits to 1 + GPIOF -> AFSEL = 0x09; + // 7. Set PUE bits to 1 to enable internal pull-up + // 8. Set DEN bits to 1 to enable data pins + GPIOF -> DEN = 0x09; +} + +void SetupPWMDEFINE(void) +{ + int load = 40000; + // 1. Enable Clock for PWM Module 1 + SYSCTL->RCGCPWM |= _PWM_MODULE1; + while((SYSCTL->PRPWM & _PWM_MODULE1)!= _PWM_MODULE1){}; + // 2. Enable and Setup Clock Divider for PWM + SYSCTL->RCC |= (1 << 20); // RCC[20]=1:USEPWMDIV + SYSCTL->RCC &= ~0x000E0000; // RCC[19:17]=000 PWMDIV + SYSCTL->RCC |= (_PWMDIV_2 << 17); // RCC[19:17]=0x04 divider=/32 + // 3. Disable PWM Generator 2 + PWM1->_2_CTL &= ~0x01; // Disable PWM Generator 2 + PWM1->_3_CTL &= ~0x01; // Disable PWM Generator 3 + // 4. Config LOAD, CMPn, GENn values + + PWM1->_2_LOAD = load; // GEN 2 A PWM 4 PF0 MOTOR + PWM1->_2_CMPA = load/2; + PWM1->_2_GENA = _PWM_LEFT_ALIG_CMPBD;//0x080C + + PWM1->_3_LOAD = load; // GEN 3 B PWM 7 PF3 LED + PWM1->_3_CMPB = load/2; + PWM1->_3_GENB = (0x02 << 10 ) | (0x03 <<2);//0x080C + // 5. Enable PWM Generator 2 + PWM1->_2_CTL |= 0x01; + PWM1->_3_CTL |= 0x01; + // 6. Enable PWM5 Output + //PWM1->ENABLE |= 1 << 4; // Enable PWM4 + //PWM1->ENABLE |= 1 << 7; // Enable PWM7 + PWM1 -> ENABLE |= 0x90; +} + +void SetupPWM(void) +{ + int load = 10000; + // 1. Enable Clock for PWM Module 1 + SYSCTL->RCGCPWM |= 0x02; + while((SYSCTL->PRPWM & 0x02)==0){}; + // 2. Enable and Setup Clock Divider for PWM + SYSCTL->RCC |= (1 << 20); // RCC[20]=1:USEPWMDIV + SYSCTL->RCC &= ~0x000E0000; // RCC[19:17]=000 PWMDIV + SYSCTL->RCC |= (0x03 << 17); // RCC[19:17]=0x04 divider=/32 + // 3. Disable PWM Generator 2 + PWM1->_2_CTL &= ~0x01; // Disable PWM Generator 2 + PWM1->_3_CTL &= ~0x01; // Disable PWM Generator 3 + // 4. Config LOAD, CMPn, GENn values + + PWM1->_2_LOAD = load; // GEN 2 A PWM 4 PF0 MOTOR + PWM1->_2_CMPA = load/2; + PWM1->_2_GENA = (0x02 << 6 ) | (0x03 <<2);//0x080C + + PWM1->_3_LOAD = load; // GEN 3 B PWM 7 PF3 LED + PWM1->_3_CMPB = load/2; + PWM1->_3_GENB = (0x02 << 10 ) | (0x03 <<2);//0x080C + // 5. Enable PWM Generator 2 + PWM1->_2_CTL |= 0x01; + PWM1->_3_CTL |= 0x01; + // 6. Enable PWM5 Output + //PWM1->ENABLE |= 1 << 4; // Enable PWM4 + //PWM1->ENABLE |= 1 << 7; // Enable PWM7 + PWM1 -> ENABLE |= 0x90; +} \ No newline at end of file diff --git a/PinPad/MyDefines.h b/PinPad/MyDefines.h new file mode 100755 index 0000000..7aa5515 --- /dev/null +++ b/PinPad/MyDefines.h @@ -0,0 +1,43 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x01 +#define _PORTB 0x02 +#define _PORTC 0x04 +#define _PORTD 0x08 +#define _PORTE 0x10 +#define _PORTF 0x20 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#endif \ No newline at end of file diff --git a/PinPad/pinpad.c b/PinPad/pinpad.c new file mode 100755 index 0000000..0e586f1 --- /dev/null +++ b/PinPad/pinpad.c @@ -0,0 +1,134 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +#include "ez123G.h" +#include "MyDefines.h" + +char str[100]; +char ReadKeyPad(); + +int main(void) +{ + PEZOBJ_LCD lcd; + uint16_t i = 0; + + // GPIO Initialization and Configuration + // 1. Enable Clock on GPIOs + SYSCTL->RCGCGPIO |= 0x1B; + // allow time for clock to stabilize + while ((SYSCTL->PRGPIO & 0x1B) != 0x1B) {}; + // 2. Unlock PD7 and/or PF0 for TM4C 123G + // 3. Config AMSEL to disable analog function + // 4. Config PCTL to select 0-GPIO + // 5. Set AFSEL bits to 0 + // 6. Set DIR to 0 for input, 1 for output + + GPIOA -> DIR = 0x00; + GPIOB -> DIR = 0x03; + GPIOD -> DIR = 0x0F; + GPIOE -> DIR = 0x3E; + + // 7. Set PUR/PDR/ODR bits to 1 to enable internal pull-up/-down resistir and/or open-drain + GPIOB -> ODR = 0x03; + GPIOE -> ODR = 0x30; + GPIOA -> PUR = 0xE0; + GPIOB -> PUR = 0x10; + + // 8. Set DEN bits to 1 to enable all + GPIOA -> DEN = 0xE0; + GPIOB -> DEN = 0x13; + GPIOD -> DEN = 0x0F; + GPIOE -> DEN = 0x3E; + + + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIOE, PIN1); + ezLCD_Connect_RSPin(lcd, GPIOE, PIN2); + ezLCD_Connect_RWPin(lcd, GPIOE, PIN3); + + ezLCD_Start(lcd); + ezLCD_ClearDisplay(lcd); + ezLCD_Position(lcd, 1, 0); + ezLCD_PrintString(lcd, "HELLO"); + + char ch; + while(1){ + ch = ReadKeyPad(); + + if (ch == '*'){ + i *= 100; + ezLCD_ClearDisplay(lcd); + } + if (ch == '#') i = 0; + if (ch >='0' && ch <= '9') i = i*10 + ch - '0'; + + sprintf(str, "%d ", i); + ezLCD_Position(lcd, 0, 0); + ezLCD_PrintString(lcd, str); + + timer_waitMillis(100); + } +} +//-------------------------------------------------------------- +char KeyPad[4][4]={ + {'1','2','3','A'}, + {'4','5','6','B'}, + {'7','8','9','C'}, + {'*','0','#','D'}}; + +char ReadKeyPad() +{ + int row; + GPIOB -> DATA |= 0x03; + GPIOE -> DATA |= 0x30; + for(row = 0; row < 4; row++){ + switch(row){ + case 0: + GPIOB -> DATA &= ~0x01; + break; + case 1: + GPIOB -> DATA |= 0x01; + GPIOB -> DATA &= ~0x02; + break; + case 2: + GPIOB -> DATA |= 0x02; + GPIOE -> DATA &= ~0x10; + break; + case 3: + GPIOE -> DATA |= 0x10; + GPIOE -> DATA &= ~0x20; + break; + } + + timer_waitMillis(10); + + if(!(GPIOB -> DATA & 0x10)) + { + while(!(GPIOB -> DATA & 0x10)){} + return KeyPad[row][0]; + } + if(!(GPIOA -> DATA & 0x20)) + { + while(!(GPIOA -> DATA & 0x20)){} + return KeyPad[row][1]; + } + if(!(GPIOA -> DATA & 0x40)) + { + while(!(GPIOA -> DATA & 0x40)){} + return KeyPad[row][2]; + } + if(!(GPIOA -> DATA & 0x80)) + { + while(!(GPIOA -> DATA & 0x80)){} + return KeyPad[row][3]; + } + + row++; + } + + return 0x0; +} \ No newline at end of file diff --git a/Report Motor Speed/MyDefines.h b/Report Motor Speed/MyDefines.h new file mode 100755 index 0000000..fb99053 --- /dev/null +++ b/Report Motor Speed/MyDefines.h @@ -0,0 +1,43 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#endif \ No newline at end of file diff --git a/Report Motor Speed/main.c b/Report Motor Speed/main.c new file mode 100755 index 0000000..fadfed4 --- /dev/null +++ b/Report Motor Speed/main.c @@ -0,0 +1,167 @@ +#include +#include +#include +#include +#include "MyDefines.h" + +#include "TM4C123GH6PM.h" + +void SetupPWM(void); +void SetupGPIO(void); +void Delay(int t); + +int main() +{ + SetupPWM(); + SetupGPIO(); + + uint32_t *sw1 = (uint32_t *)(((char*)GPIOF) + (_PIN4 << 2)); + uint32_t *sw2 = (uint32_t *)(((char*)GPIOF) + (_PIN0 << 2)); + + uint32_t *cwturn = (uint32_t *)(((char*)GPIOA) + (_PIN3 << 2)); + uint32_t *acwturn = (uint32_t *)(((char*)GPIOA) + (_PIN2 << 2)); + *cwturn = 0xFF; + *acwturn = 0x00; + + bool countup = true; + bool turn_clockwise = true; + + *cwturn = 0xFF; + *acwturn = 0x00; + PWM0 -> ENABLE |= _PWM3; + PWM1 -> ENABLE |= _PWM7; + double cmp_percent = 0.9; //Start at 10% Duty Cycle + + bool sw1currstate; + bool sw1prevstate = false; + + bool sw2currstate; + bool sw2prevstate = false; + + while(1){ + + if(!(*sw1)) sw1currstate = true; + else sw1currstate = false; + if(!(*sw2)) sw2currstate = true; + else sw2currstate = false; + + //DUTY CYCLE CHANGE + if(!sw1currstate && sw1prevstate){ //Falling Edge Triggered + if(cmp_percent == 1){ //count down at 100% duty cycle + countup = false; + } + if(cmp_percent == 0){ //count up at 0% duty cycle + countup = true; + } + if(countup == true){ //add 10% duty cycle + cmp_percent -= 0.1; + }else{ //remove 10% duty cycle + cmp_percent += 0.1; + } + PWM1->_3_CMPB = 20000 - 20000*cmp_percent; + PWM0->_1_CMPB = 20000 - 20000*cmp_percent; + } + + //MOTOR DIRECTION CHANGE + if(!sw2currstate && sw2prevstate){ //Falling Edge Triggered + + PWM0->_1_CMPB = 20000; //Set Duty Cycle 0% + PWM1->_3_CMPB = 20000; + + Delay(2000); //Wait 2 Seconds + + if(turn_clockwise){ + *cwturn = 0x00; + *acwturn = 0xFF; + turn_clockwise = false; + }else{ + *cwturn = 0xFF; + *acwturn = 0x00; + turn_clockwise = true; + } + PWM1->_3_CMPB = 20000*cmp_percent; + PWM0->_1_CMPB = 20000*cmp_percent; + } + sw1prevstate = sw1currstate; + sw2prevstate = sw2currstate; + } +} + + +void SetupGPIO(void){ + + // Config for GPIO + // 1. Enable Clock on GPIOF + SYSCTL->RCGCGPIO = (_PORTA|_PORTB|_PORTF); + // allow time for clock to stabilize + while((SYSCTL->PRGPIO & (_PORTA|_PORTB|_PORTF)) != (_PORTA|_PORTB|_PORTF)){}; + + // 2. Unlock GPIO + GPIOF -> LOCK = 0x4C4F434B; + GPIOF -> CR |= _PIN0; + // 3. Clear AMSEL to disable analog + //GPIOA -> AMSEL = 0x0; + //GPIOB -> AMSEL = 0x0; + //GPIOF -> AMSEL = 0x0; + // 4. Config PCTL to select GPIO + GPIOA -> PCTL = 0x0; + GPIOB -> PCTL = 0x00400000; + GPIOF -> PCTL = 0x00005000; + // 5. Set DIR to 0 for input, 1 for output + GPIOA -> DIR |= _PIN2|_PIN3; + GPIOB -> DIR |= _PIN5; + GPIOF -> DIR |= _PIN3; + // 6. Enable AFSEL bits to 1 + GPIOA -> AFSEL = 0x0; + GPIOB -> AFSEL = _PIN5; + GPIOF -> AFSEL = _PIN3; + // 7. Set PUE bits to 1 to enable internal pull-up + GPIOF -> PUR = _PIN0|_PIN4; + // 8. Set DEN bits to 1 to enable data pins + GPIOA -> DEN |= _PIN2|_PIN3; + GPIOB -> DEN |= _PIN5; + GPIOF -> DEN |= _PIN0|_PIN3|_PIN4; +} + + + +void SetupPWM(void) +{ + int load = 20000; + // 1. Enable Clock for PWM Module 1 + SYSCTL->RCGCPWM |= (_PWM_MODULE0|_PWM_MODULE1); + while((SYSCTL->PRPWM & (_PWM_MODULE0|_PWM_MODULE1))==0){}; + + // 2. Enable and Setup Clock Divider for PWM + SYSCTL->RCC |= (1 << 20); // RCC[20]=1:USEPWMDIV + SYSCTL->RCC &= ~0x000E0000; // RCC[19:17]=000 PWMDIV + SYSCTL->RCC |= (_PWMDIV_4 << 17); // RCC[19:17]=0x04 divider=/32 + + // 3. Disable PWM Generator 2 + PWM0->_1_CTL &= ~0x01; // Disable PWM Generator 2 + PWM1->_3_CTL &= ~0x01; // Disable PWM Generator 2 + + // 4. Config LOAD, CMPn, GENn values + PWM0->_1_LOAD = load; // M0 GEN 1 B PWM 3 PB5 MOTOR + PWM0->_1_CMPB = (load - load*0.1); + PWM0->_1_GENB = (0x02 << 10 ) | (0x03 <<2);//0x080C + + PWM1->_3_LOAD = load; // M1 GEN 3 B PWM 7 PF3 LED + PWM1->_3_CMPB = (load - load*0.1); + PWM1->_3_GENB = (0x02 << 10 ) | (0x03 <<2);//0x080C + + // 5. Enable PWM Generator 2 + PWM0->_1_CTL |= 0x01; + PWM1->_3_CTL |= 0x01; + + // 6. Enable PWM5 Output + +} + +void Delay(int t) +{ + volatile int i, j; + for (i = 0; i < t; i++) + for (j = 0; j < 3180; j++) + {}; +} \ No newline at end of file diff --git a/State Machine/MyDefines.h b/State Machine/MyDefines.h new file mode 100755 index 0000000..fb99053 --- /dev/null +++ b/State Machine/MyDefines.h @@ -0,0 +1,43 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#endif \ No newline at end of file diff --git a/State Machine/main.c b/State Machine/main.c new file mode 100755 index 0000000..ea29fd8 --- /dev/null +++ b/State Machine/main.c @@ -0,0 +1,188 @@ +#include "MyDefines.h" +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" + + typedef enum STATES { + S_0, + S_1, + S_2, + S_3, +}STATES; + +void SetGPIO(void); +void GPIOF_Handler (void); +void Setup_GPIOIRQ(void); + +volatile bool sw1; + + uint32_t *led1 =(uint32_t *) (((char*)GPIOF) +(_PIN1 << 2)); + uint32_t *led2 =(uint32_t *) (((char*)GPIOF) +(_PIN3 << 2)); + +typedef struct STATELIST{ + int led1; + int led2; + struct STATELIST *next; + +}STATELIST; + +int main() +{ + STATES currState = S_0; + + STATELIST state[] = { + {0, 0, &state[1]}, //s0 + {0xFF, 0, &state[2]}, //s1 + {0xFF, 0xFF, &state[3]}, //s2 + {0, 0xFF, &state[0]}, //s3 + }; + + SetGPIO(); + Setup_GPIOIRQ (); + +//Moore State Machine +while(1){ + switch (currState){ + case S_0: + *led1 = 0x00; + *led2 = 0x00; + if(sw1){ + sw1=false; + currState = S_1; + } + break; + case S_1: + *led1 = 0x01; + *led2 = 0x00; + if(sw1){ + sw1=false; + currState = S_2; + } + break; + case S_2: + *led1 = 0x01; + *led2 = 0x01; + if(sw1){ + sw1=false; + currState = S_3; + } + break; + case S_3: + *led1 = 0x00; + *led2 = 0x01; + if(sw1){ + sw1=false; + currState = S_0; + } + break; + + } + } + +//Mealy State Machine +while(1){ + switch (currState){ + case S_0: + if(sw1){ + sw1=false; + currState = S_1; + *led1 = 0x00; + *led2 = 0x00; + } + break; + case S_1: + if(sw1){ + sw1=false; + currState = S_2; + *led1 = 0x01; + *led2 = 0x00; + } + break; + case S_2: + if(sw1){ + sw1=false; + currState = S_3; + *led1 = 0x01; + *led2 = 0x01; + } + break; + case S_3: + if(sw1){ + sw1=false; + currState = S_0; + *led1 = 0x00; + *led2 = 0x01; + } + break; + + } + } + +//MOORE STATE WITH LINKED LIST +STATELIST *pState = &state[0]; +while(1){ + *led1 = pState->led1; + *led2 = pState->led2; + if(sw1){ + + } +} +} +void SetGPIO(void) +{ + // Config for GPIO + // 1. Enable Clock on GPIOF + SYSCTL->RCGCGPIO = _PORTF; + // allow time for clock to stabilize + while((SYSCTL->PRGPIO & _PORTF) != _PORTF){}; + + // 2. Unlock GPIO + + // 3. Clear AMSEL to disable analog + // 4. Config PCTL to select GPIO + GPIOF->PCTL= 0x00; + // 5. Set DIR to 0 for input, 1 for output + GPIOF->DIR= _PIN1 | _PIN2; + // 6. Enable AFSEL bits to 1 + + // 7. Set PUE bits to 1 to enable internal pull-up + GPIOF->PUR= _PIN4; + // 8. Set DEN bits to 1 to enable data pins + GPIOF->DEN= _PIN1 | _PIN4 | _PIN2; +} + +void Setup_GPIOIRQ() +{ + uint16_t n,m,r,r2; + // 1. Disable IRQ in GPIOIM register + GPIOF->IM &= ~(_PIN4); + // 2. Configure IRQ Type (0=edge, 1=level) in the GPIOIS register + GPIOF->IS &= ~(_PIN4); + // 3. Configure GPIOIBE (0=single edge, 1=both edge), + // GPIOIEV (0=low level or falling edge, 1=high level or rising edge) + GPIOF->IBE &= ~(_PIN4); + GPIOF->IEV &= ~(_PIN4); + // 4. Clear the GPIORIS register + GPIOF->ICR |= _PIN4; + // 5. Enable IRQ in the GPIOIM register + GPIOF->IM |= _PIN4; + // 6. Set priority in the NVIC + n = 30/4; + r = 21; + m = 30/32; r2 = 30 % 32; + NVIC->IP[n] |= (3 << r); + // 7. Enable IRQ in the NVIC + NVIC->ISER[m] |= (1 << r2); +} + uint32_t *pn1 =(uint32_t *) (((char*)GPIOF) +(_PIN4 << 2)); + +void GPIOF_Handler()//Interrupt service +{ + GPIOF ->ICR |= _PIN4; // Clear Interrupt flag + sw1=true; + //*pn1 ^= _PIN1; + +} \ No newline at end of file diff --git a/TemplateCode_01.c b/TemplateCode_01.c new file mode 100755 index 0000000..744c0d6 --- /dev/null +++ b/TemplateCode_01.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +#include "MyDefines.h" +#include "ez123G.h" +void initGPIO(void); +void initTimer(void); + +uint32_t deltaT; + +void UltraSonic(); +uint32_t MeasureD(); + +int main(void) +{ + Setup_123G_80MHz(); //Setup System Clock to 80MHz + initTimer(); + initGPIO(); + + for(;;){ + + } +} + +// trig->pf3 for 123G +uint32_t *pf3; +void UltraSonic(){ + uint32_t pulseWidth; + //Set "Trig" pin to low for 500ms + *pf3 = 00; + timer_waitMillis(500); //Waiting for 500 ms + //Set "trig" pin to high for 10 us + *pf3 = _PIN3; //*pf3 = 0xFF + timer_waitMicros(10); + //Call MeasureD() + pulseWidth = MeasureD(); + //Calculate deltaT + + //Calculate distance in cm +} + +uint32_t MeasureD(){ + uint32_t highEdge, lowEdge; + uint32_t pulseCounter; + + //Capture first edge -- rising edge + + //Clear TimerA Capture mode event by clearing bit on ICR register + TIMER1->ICR |= (1 << 2); //CAECINT + //Waiting for capture signal by check RIS register + while ((TIMER1->RIS & (1<<2)) != (1<<2)){} + //read the high Edge from the TAR register + highEdge = (TIMER1->TAR) | (TIMER1->TAPS << 16); + + //capture second edge -- falling edge + //Clear TimerA Capture mode event by clearing bit on ICR register + TIMER1->ICR |= (1 << 2); //CAECINT + //Waiting for capture signal by check RIS register + while ((TIMER1->RIS & (1<<2)) != (1<<2)){} + //read the lowEdge from the TAR register + lowEdge = (TIMER1->TAR) | (TIMER1->TAPS << 16); + if(highEdge > lowEdge) + deltaT = highEdge - lowEdge; + else + deltaT = highEdge + (0xFFFFFF)- lowEdge; + + return pulseCounter; +} + +void initTimer(void){ + //Enable Clock On Timer Module + SYSCTL->RCGCTIMER |= _TIMER1; + while((SYSCTL->PRTIMER & (_TIMER1)) != (_TIMER1)){} + //Disable Timer A/B + TIMER1->CTL &= ~_TIMERA_ENABLE; + //Configure Timer + TIMER1->CFG = 0x04; //Split into two 16-bit timer + //Configure Timer A Mode + TIMER1->TAMR = _TIMERA_COUNTDOWN | _TIMERA_CAPTURE | _TIMERA_EDGE_TIME; + //Configure Timer Event Mode + TIMER1->CTL &= ~(0x03 << 2); + TIMER1->CTL |= _TIMERA_BOTH_EDGES; + //Config Load + TIMER1->TAILR = 0xffff; + //Set the prescaler to 15 + TIMER1->TAPR = 0xff; + TIMER1->IMR = 0; // IMR = interrupt mask = 0, disabled all timer IRQ + //Enable TImer A + TIMER1->CTL |= _TIMERA_ENABLE; +} +void initGPIO(void){ + + // GPIO Configuration + // 1. Enable Clock on GPIOF[3:1] (SYSCTL_RCGCGPIO) + SYSCTL->RCGCGPIO = 0x20; + // allow time for clock to stabilize (SYSCTL_PRGPIO) + while((SYSCTL->PRGPIO & 0x20) != 0x20){}; + // 2. Unlock GPIOC[3:0], GIOD[7] and GPIOF[0] on TM4C123G (GPIOn->LOCK and GPIOn->CR) NOT USING + GPIOF->LOCK = 0x4C4F434B; + GPIOF->CR |= 0x01; + // 3. Clear AMSEL to disable analog + GPIOF->AMSEL = 0x00; + // 4. Config PCTL to select GPIO + GPIOF->PCTL = 0x0000; + //or + //GPIOF->PCTL &= 0xFFF00FFF; + // 5. Set DIR to 0 for input, 1 for output + GPIOF->DIR = 0x0E; // PF3,PF2,PF1 for Output + // 6. Enable AFSEL bits to 1 + GPIOF->AFSEL = 0x00; + // 7. Set PUR bits to 1 to enable internal pull-up + GPIOF->PUR = 0x11; + // 8. Set DEN bits to 1 to enable data pins + GPIOF->DEN = 0x1F; // Enable digital pin on PF3,PF2,PF1 +} diff --git a/Ultrasonic/MyDefines.h b/Ultrasonic/MyDefines.h new file mode 100755 index 0000000..3e69e6f --- /dev/null +++ b/Ultrasonic/MyDefines.h @@ -0,0 +1,62 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#define _TIMER0 0x01 +#define _TIMER1 0x02 +#define _TIMER2 0x04 +#define _TIMER3 0x08 +#define _TIMER4 0x10 +#define _TIMER5 0x20 +#define _TIMER6 0x40 +#define _TIMER7 0x80 + +#define _TIMERA_ENABLE (1 << 0) +#define _TIMERB_ENABLE (1 << 8) +#define _TIMERA_COUNTUP (1 << 4) +#define _TIMERA_COUNTDOWN (0 << 4) +#define _TIMERA_CAPTURE 0x03 +#define _TIMERA_EDGE_TIME (1 << 2) +#define _TIMERA_POSITIVE_EDGE 0 +#define _TIMERA_NEGATIVE_EDGE (1 << 2) +#define _TIMERA_BOTH_EDGES (3 << 2) + +#endif \ No newline at end of file diff --git a/Ultrasonic/main.c b/Ultrasonic/main.c new file mode 100755 index 0000000..c28fc3f --- /dev/null +++ b/Ultrasonic/main.c @@ -0,0 +1,165 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +#include "ez123G.h" +#include "MyDefines.h" + +char str[100]; + +void Setup_GPIO(void); +uint32_t getWidth(); +uint32_t measureD(void); +void Setup_Timer(void); + +int main(void) +{ + uint16_t i = 0; + + Setup_123G_80MHz(); + Setup_Timer(); + Setup_GPIO(); + + PEZOBJ_LCD lcd; + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIOE, PIN1); + ezLCD_Connect_RSPin(lcd, GPIOE, PIN2); + ezLCD_Connect_RWPin(lcd, GPIOE, PIN3); + + double time; + double distance; + uint32_t widthArray[3]; + uint32_t avgWidth; + ezLCD_Start(lcd); + ezLCD_LoadVerticalBargraphFonts(lcd); + while(1){ + + widthArray[0] = getWidth(); + //widthArray[1] = getWidth(); + //widthArray[2] = getWidth(); + //avgWidth = (widthArray[0]+widthArray[1]+widthArray[2])/3; + time = widthArray[0] * (1.25E-8); + distance = time * (34000/2); + + ezLCD_Position(lcd, 0, 0); + sprintf(str,"%d: width: %2d", ++i, widthArray[0]); + ezLCD_PrintString(lcd, str); + ezLCD_Position(lcd, 1, 0); + sprintf(str,"dist(cm): %6.2lf cm \n\r", distance); + ezLCD_PrintString(lcd, str); + //timer_waitMillis(100); + } + +} + +uint32_t getWidth() +{ + uint32_t pulseWidth = 0; + uint32_t *trig = (uint32_t *)(((char*)GPIOF) + (_PIN3 << 2)); + // Set "Trig" pin to Low + *trig = 0x00; + timer_waitMillis(500); // Waiting for 500ms + // Set "Trig" pin to High for 10ns + *trig = 0xFF; + timer_waitMicros(10); // Waiting for 10ns + // Set "Trig" pin to Low + *trig = 0x00; + pulseWidth = measureD(); // Call measureD() to get the delta t on the "echo" pin + // Calculate distance + + return pulseWidth; +} + +uint32_t measureD(void) +{ + uint32_t highEdge,lowEdge; + uint32_t deltaT; + + /*Capture firstEgde i.e. rising edge*/ + //1. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1 -> ICR = 1 << 2; + //2. Waiting for capture rising edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while((TIMER1 -> RIS & (1 << 2)) != (1 << 2)){} + //3. Read the highEdge from GPTMTAR (TIMER1->TAR) registers + highEdge = TIMER1 -> TAR; + /*Capture secondEgde i.e. falling edge*/ + //4. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1 -> ICR = 1 << 2; + //5. Waiting for capture falling edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while((TIMER1 -> RIS & (1 << 2)) != (1 << 2)){} + //6. Read the lowEdge from GPTMTAR (TIMERa->TAR) registers + lowEdge = TIMER1 -> TAR; + //7. Calculate deltaT = highEdge - lowEdge + // Note: the deltaT must large than zero, cannot be negative value + if(highEdge > lowEdge){ + deltaT = highEdge - lowEdge; + }else{ + deltaT = 0xFFFFFFFF - (lowEdge - highEdge); + } + return deltaT; +} + +void Setup_Timer(void) +{ + //* PF2: T1CCP0 (PCTL=07) both edge (edge-time mode) + + // 1 . Enable Clock for TIMER Module + SYSCTL->RCGCTIMER |= (_TIMER1); + while ((SYSCTL->PRTIMER & (_TIMER1)) != (_TIMER1)) {} + // 2. Disable TIMER + TIMER1->CTL &= ~(_TIMERA_ENABLE); // Disable TimerA & TimerB + // 3. Configure TIMER + TIMER1->CFG = 0x04; // Split into two 16-bit timers + // 4. Configure Timer n Mode: GPTMTAMR + TIMER1->TAMR = (_TIMERA_COUNTDOWN|_TIMERA_EDGE_TIME|_TIMERA_CAPTURE); + // 5. Configure Timer Event Mode: rising-, falling-, or both-edges + TIMER1->CTL &= ~(0x03 << 2); + TIMER1->CTL |= (_TIMERA_BOTH_EDGES); // Both edges + // 6. Configure Load + TIMER1->TAILR = 0xFFFF; + //Set the prescaler to 0xFF + TIMER1->TAPR = 0xFF; + TIMER1->IMR = 0; + // 7. Enable GPTM Timer + TIMER1->CTL |= _TIMERA_ENABLE; // Enable TimerB +} + +void Setup_GPIO(void) +{ + + // GPIO Initialization and Configuration + // 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO) + SYSCTL->RCGCGPIO |= _PORTD|_PORTE|_PORTF; + // allow time for clock to stabilize (SYSCTL->PRGPIO) + while((SYSCTL->PRGPIO & (_PORTD|_PORTE|_PORTF)) != (_PORTD|_PORTE|_PORTF)){}; + // 2. Unlock PD7 and PF0 on TM4C123G; or PD7 on TM4C1294 (GPIO->LOCK and GPIO->CR) + // 3. GPIO Analog Mode Select (GPIOAMSEL) + GPIOD -> AMSEL = 0x0; + GPIOE -> AMSEL = 0x0; + GPIOF -> AMSEL = 0x0; + // 4. GPIO Port COntrol (GPIOPCTL) + GPIOD -> PCTL = 0x0; + GPIOE -> PCTL = 0x0; + GPIOF -> PCTL = 0x00000700; + // 5. Clear AFSEL bits to 0 to select regular I/O + GPIOD -> AFSEL = 0x0; + GPIOE -> AFSEL = 0x0; + GPIOF -> AFSEL |= _PIN2; + // 6. GPIO Pin Direction (GPIODIR) 0 for input, 1 for output + GPIOD -> DIR |= _PIN0|_PIN1|_PIN2|_PIN3; + GPIOE -> DIR |= _PIN1|_PIN2|_PIN3; + GPIOF -> DIR |= _PIN3; + // 7. Set PUR bits to 1 to enable internal pull-up resistor + // 8. Set DEN bits to 1 to enable data pins + GPIOD -> DEN |= _PIN0|_PIN1|_PIN2|_PIN3; + GPIOE -> DEN |= _PIN1|_PIN2|_PIN3; + GPIOF -> DEN |= _PIN2|_PIN3; + +} \ No newline at end of file diff --git a/addserieslcd.txt b/addserieslcd.txt new file mode 100755 index 0000000..a84d920 --- /dev/null +++ b/addserieslcd.txt @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +#include "ez123G.h" + +char str[100]; + +int main(void) +{ + PEZOBJ_LCD lcd; + uint16_t i = 0; + + // Enable GPIOD[3:0] and GPIOE[3:1] + // Config for GPIOD and GPIOE + // 1. Enable Clock on GPIOs + SYSCTL->RCGCGPIO |= 0x18; + // allow time for clock to stabilize + while ((SYSCTL->PRGPIO & 0x18) != 0x18) {}; + // 2. Unlock PD7 + GPIOD->LOCK = 0x4C4F434B; + GPIOD->CR |= 0x80; + + // 3. Config AMSEL to disable analog function + GPIOD -> AMSEL = 0x00; + GPIOE -> AMSEL = 0x00; + // 4. Config PCTL to select GPIO + GPIOD -> PCTL = 0x00; + GPIOE -> PCTL = 0x00; + // 5. Set DIR to 0 for input, 1 for output + GPIOD -> DIR |= 0x0F; + GPIOE -> DIR |= 0x0E; + + // 6. Set AFSEL bits to 0 + GPIOD -> AFSEL = 0x00; + GPIOE -> AFSEL = 0x00; + // 7. Set PUE bits to 1 to enable internal pull-up (Skipped) + // 8. Set DEN bits to 1 to enable data pins + GPIOD -> DEN |= 0x0F; + GPIOE -> DEN |= 0x0E; + + + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIOE, PIN1); + ezLCD_Connect_RSPin(lcd, GPIOE, PIN2); + ezLCD_Connect_RWPin(lcd, GPIOE, PIN3); + + ezLCD_Start(lcd); + ezLCD_LoadVerticalBargraphFonts(lcd); + + ezLCD_ClearDisplay(lcd); + int a = 4; + int total = 0; + while(a <= 120){ + ezLCD_Position(lcd, 0, 0); + sprintf(str, "+ %d", a); + total += a; + ezLCD_PrintString(lcd, str); + ezLCD_Position(lcd, 1, 0); + sprintf(str, "=%d", total); + ezLCD_PrintString(lcd, str); + a += 2; + timer_waitMillis(50); + } +} \ No newline at end of file diff --git a/average.txt b/average.txt new file mode 100755 index 0000000..4741780 --- /dev/null +++ b/average.txt @@ -0,0 +1,81 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +#include "ez123G.h" + +char str[100]; + +int main(void) +{ + PEZOBJ_LCD lcd; + uint16_t i = 0; + int array[]= {10, 35, 65, 43, 32, 86, 44, 9, 18}; + + // Enable GPIOD[3:0] and GPIOE[3:1] + // Config for GPIOD and GPIOE + // 1. Enable Clock on GPIOs + SYSCTL->RCGCGPIO |= 0x18; + // allow time for clock to stabilize + while ((SYSCTL->PRGPIO & 0x18) != 0x18) {}; + // 2. Unlock PD7 + GPIOD->LOCK = 0x4C4F434B; + GPIOD->CR |= 0x80; + + // 3. Config AMSEL to disable analog function + GPIOD -> AMSEL = 0x00; + GPIOE -> AMSEL = 0x00; + // 4. Config PCTL to select GPIO + GPIOD -> PCTL = 0x00; + GPIOE -> PCTL = 0x00; + // 5. Set DIR to 0 for input, 1 for output + GPIOD -> DIR |= 0x0F; + GPIOE -> DIR |= 0x0E; + + // 6. Set AFSEL bits to 0 + GPIOD -> AFSEL = 0x00; + GPIOE -> AFSEL = 0x00; + // 7. Set PUE bits to 1 to enable internal pull-up (Skipped) + // 8. Set DEN bits to 1 to enable data pins + GPIOD -> DEN |= 0x0F; + GPIOE -> DEN |= 0x0E; + + + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIOE, PIN1); + ezLCD_Connect_RSPin(lcd, GPIOE, PIN2); + ezLCD_Connect_RWPin(lcd, GPIOE, PIN3); + + ezLCD_Start(lcd); + ezLCD_LoadVerticalBargraphFonts(lcd); + + ezLCD_ClearDisplay(lcd); + + int j = 0; + int a = 0, s = 0, M = array[0], m = array[0]; + int total = 0; + while(j <= 8){ + ezLCD_ClearDisplay(lcd); + s += array[j]; + if(array[j] > M) M = array[j]; + if(array[j] < m) m = array[j]; + a = s / (j + 1); + ezLCD_Position(lcd, 0, 0); + sprintf(str, "S=%d", s); + ezLCD_PrintString(lcd, str); + ezLCD_Position(lcd, 0, 8); + sprintf(str, "M=%d", M); + ezLCD_PrintString(lcd, str); + ezLCD_Position(lcd, 1, 0); + sprintf(str, "A=%d", a); + ezLCD_PrintString(lcd, str); + ezLCD_Position(lcd, 1, 8); + sprintf(str, "m=%d", m); + ezLCD_PrintString(lcd, str); + timer_waitMillis(500); + j++; + } +} \ No newline at end of file diff --git a/ezTivaLib/Defines.h b/ezTivaLib/Defines.h new file mode 100755 index 0000000..18ae81e --- /dev/null +++ b/ezTivaLib/Defines.h @@ -0,0 +1,39 @@ + +#ifndef __DEFINES_H +#define __DEFINES_H + +#include +#include +#include +#include + +/* +#if defined(TM4C123GH6PM) + #include "TM4C123GH6PM.h" +#elif defined(TM4C1294NCPDT) + #include "TM4C1294NCPDT.h" +#endif +*/ + + +#define PIN0 0x01 +#define PIN1 0x02 +#define PIN2 0x04 +#define PIN3 0x08 +#define PIN4 0x10 +#define PIN5 0x20 +#define PIN6 0x40 +#define PIN7 0x80 + +#define BIT0 0x01 +#define BIT1 0x02 +#define BIT2 0x04 +#define BIT3 0x08 +#define BIT4 0x10 +#define BIT5 0x20 +#define BIT6 0x40 +#define BIT7 0x80 + + + +#endif diff --git a/ezTivaLib/ez123G.h b/ezTivaLib/ez123G.h new file mode 100755 index 0000000..b8e5b26 --- /dev/null +++ b/ezTivaLib/ez123G.h @@ -0,0 +1,14 @@ +#ifndef __EZ123G_LIB__H +#define __EZ123G_LIB__H + +#include +#include +#include +#include +#include "Defines.h" +#include "ezTimer.h" +#include "ezLCD.h" + +#endif + + diff --git a/ezTivaLib/ez123GLIB.lib b/ezTivaLib/ez123GLIB.lib new file mode 100755 index 0000000..2a6f5c7 Binary files /dev/null and b/ezTivaLib/ez123GLIB.lib differ diff --git a/ezTivaLib/ezLCD.h b/ezTivaLib/ezLCD.h new file mode 100755 index 0000000..9e60360 --- /dev/null +++ b/ezTivaLib/ezLCD.h @@ -0,0 +1,72 @@ +#ifndef __EZLCD_LIB__H +#define __EZLCD_LIB__H + +#if defined(TM4C123GH6PM) + #include "TM4C123GH6PM.H" + typedef GPIOA_Type GPIO_Port; + typedef GPIOA_Type* PGPIO_Port; +# +#elif defined(TM4C1294NCPDT) + #include "TM4C1294NCPDT.h" + typedef GPIOA_AHB_Type GPIO_Port; + typedef GPIOA_AHB_Type* PGPIO_Port; +#else + #error "The Device NOT specified" +#endif + +#include +#include +#include +#include + +#include // memset +#include "ezTimer.h" + +//typedef GPIOA_AHB_Type GPIO_Port; +//typedef GPIOA_AHB_Type* PGPIO_Port; + + +typedef enum { + PIN_3_0, + PIN_7_4 +} EZLCD_DATAPORT_PIN; + +typedef struct EZOBJ_LCD{ + void* PrivateData; +} EZOBJ_LCD; +typedef EZOBJ_LCD * PEZOBJ_LCD; + +PEZOBJ_LCD ezLCD_Create(void); + +void ezLCD_Connect_DataPort(PEZOBJ_LCD lcd, PGPIO_Port port, EZLCD_DATAPORT_PIN mode); +void ezLCD_Connect_ENPin(PEZOBJ_LCD lcd, PGPIO_Port port, uint8_t enpin); +void ezLCD_Connect_RSPin(PEZOBJ_LCD lcd, PGPIO_Port port, uint8_t rspin); +void ezLCD_Connect_RWPin(PEZOBJ_LCD lcd, PGPIO_Port port, uint8_t rwpin); + +bool ezLCD_Start(PEZOBJ_LCD lcd); +void ezLCD_Enable(PEZOBJ_LCD lcd); +void ezLCD_DisplayOff(PEZOBJ_LCD lcd); +void ezLCD_DisplayOn(PEZOBJ_LCD lcd); + +void ezLCD_ClearDisplay(PEZOBJ_LCD lcd); +void ezLCD_Position(PEZOBJ_LCD lcd, uint8_t row, uint8_t column); +void ezLCD_LoadVerticalBargraphFonts(PEZOBJ_LCD lcd); +void ezLCD_LoadHorizontalBargraphFonts(PEZOBJ_LCD lcd); + +void ezLCD_LoadCustomFonts(PEZOBJ_LCD lcd, uint8_t const customData[]); + +void ezLCD_DrawHorizontalBG(PEZOBJ_LCD lcd, uint8_t row, uint8_t column, uint8_t maxCharacters, uint8_t value); +void ezLCD_DrawVerticalBG(PEZOBJ_LCD lcd, uint8_t row, uint8_t column, uint8_t maxCharacters, uint8_t value); + +void ezLCD_PrintString(PEZOBJ_LCD lcd, char const str[]); +void ezLCD_PrintInt8(PEZOBJ_LCD lcd, uint8_t value); +void ezLCD_PrintInt16(PEZOBJ_LCD lcd, uint16_t value); +void ezLCD_PrintInt32(PEZOBJ_LCD lcd, uint32_t value); +void ezLCD_PrintNumber(PEZOBJ_LCD lcd, uint16_t value); +void ezLCD_Print32Number(PEZOBJ_LCD lcd, uint32_t value); + +#define ezLCD_PrintDecUint16(lcd,x) ezLCD_PrintNumber(lcd,x) +#define ezLCD_PrintHexUint8(lcd,x) ezLCD_PrintInt8(lcd,x) +#define ezLCD_PrintHexUint16(lcd,x) ezLCD_PrintInt16(lcd,x) + +#endif diff --git a/ezTivaLib/ezTimer.h b/ezTivaLib/ezTimer.h new file mode 100755 index 0000000..043a4d4 --- /dev/null +++ b/ezTivaLib/ezTimer.h @@ -0,0 +1,33 @@ +#ifndef __EZ_1294_LIB_H +#define __EZ_1294_LIB_H + +#include +#include +#include +#include +#include "Defines.h" + + + +#if defined(TM4C123GH6PM) + #include "TM4C123GH6PM.h" +#elif defined(TM4C1294NCPDT) + #include "TM4C1294NCPDT.h" +#endif + + +typedef uint32_t clock_t; + +#if defined(TM4C123GH6PM) + void Setup_123G_80MHz(void); +#elif defined(TM4C1294NCPDT) + void Setup_1294_80MHz(void); +#endif + + +void timer_waitMillis(uint32_t millis); +void timer_waitMicros(uint16_t micros); + + + +#endif diff --git a/firstdraft.txt b/firstdraft.txt new file mode 100755 index 0000000..874f67a --- /dev/null +++ b/firstdraft.txt @@ -0,0 +1,183 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" +#include "ez123G.h" +#include "MyDefines.h" + +char str[100]; + +void Setup_GPIO(void); +uint32_t getWidth(); +uint32_t measureD(void); +void Setup_Timer(void); +void Setup_PWM(void); + +int main(void) +{ + uint16_t i = 0; + + Setup_123G_80MHz(); + Setup_Timer(); + Setup_PWM(); + Setup_GPIO(); + + PEZOBJ_LCD lcd; + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIOE, PIN1); + ezLCD_Connect_RSPin(lcd, GPIOE, PIN2); + ezLCD_Connect_RWPin(lcd, GPIOE, PIN3); + + double time; + double distance; + uint32_t widthArray[3]; + uint32_t avgWidth; + ezLCD_Start(lcd); + ezLCD_LoadVerticalBargraphFonts(lcd); + + +} + +uint32_t getWidth() +{ + uint32_t pulseWidth = 0; + uint32_t *trig = (uint32_t *)(((char*)GPIOF) + (_PIN3 << 2)); + // Set "Trig" pin to Low + *trig = 0x00; + timer_waitMillis(500); // Waiting for 500ms + // Set "Trig" pin to High for 10ns + *trig = 0xFF; + timer_waitMicros(10); // Waiting for 10ns + // Set "Trig" pin to Low + *trig = 0x00; + pulseWidth = measureD(); // Call measureD() to get the delta t on the "echo" pin + // Calculate distance + + return pulseWidth; +} + +uint32_t measureD(void) +{ + uint32_t highEdge,lowEdge; + uint32_t deltaT; + + /*Capture firstEgde i.e. rising edge*/ + //1. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1 -> ICR = 1 << 2; + //2. Waiting for capture rising edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while((TIMER1 -> RIS & (1 << 2)) != (1 << 2)){} + //3. Read the highEdge from GPTMTAR (TIMER1->TAR) registers + highEdge = TIMER1 -> TAR; + /*Capture secondEgde i.e. falling edge*/ + //4. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1 -> ICR = 1 << 2; + //5. Waiting for capture falling edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while((TIMER1 -> RIS & (1 << 2)) != (1 << 2)){} + //6. Read the lowEdge from GPTMTAR (TIMERa->TAR) registers + lowEdge = TIMER1 -> TAR; + //7. Calculate deltaT = highEdge - lowEdge + // Note: the deltaT must large than zero, cannot be negative value + if(highEdge > lowEdge){ + deltaT = highEdge - lowEdge; + }else{ + deltaT = 0xFFFFFFFF - (lowEdge - highEdge); + } + return deltaT; +} + +void Setup_Timer(void) +{ + //* PF2: T1CCP0 (PCTL=07) both edge (edge-time mode) + + // 1 . Enable Clock for TIMER Module + SYSCTL->RCGCTIMER |= (_TIMER1); + while ((SYSCTL->PRTIMER & (_TIMER1)) != (_TIMER1)) {} + // 2. Disable TIMER + TIMER1->CTL &= ~(_TIMERA_ENABLE); // Disable TimerA & TimerB + // 3. Configure TIMER + TIMER1->CFG = 0x04; // Split into two 16-bit timers + // 4. Configure Timer n Mode: GPTMTAMR + TIMER1->TAMR = (_TIMERA_COUNTDOWN|_TIMERA_EDGE_TIME|_TIMERA_CAPTURE); + // 5. Configure Timer Event Mode: rising-, falling-, or both-edges + TIMER1->CTL &= ~(0x03 << 2); + TIMER1->CTL |= (_TIMERA_BOTH_EDGES); // Both edges + // 6. Configure Load + TIMER1->TAILR = 0xFFFF; + //Set the prescaler to 0xFF + TIMER1->TAPR = 0xFF; + TIMER1->IMR = 0; + // 7. Enable GPTM Timer + TIMER1->CTL |= _TIMERA_ENABLE; // Enable TimerB +} + +void Setup_GPIO(void) +{ + + // GPIO Initialization and Configuration + // 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO) + SYSCTL->RCGCGPIO |= _PORTD|_PORTE|_PORTF; + // allow time for clock to stabilize (SYSCTL->PRGPIO) + while((SYSCTL->PRGPIO & (_PORTD|_PORTE|_PORTF)) != (_PORTD|_PORTE|_PORTF)){}; + // 2. Unlock PD7 and PF0 on TM4C123G; or PD7 on TM4C1294 (GPIO->LOCK and GPIO->CR) + // 3. GPIO Analog Mode Select (GPIOAMSEL) + GPIOD -> AMSEL = 0x0; + GPIOE -> AMSEL = 0x0; + GPIOF -> AMSEL = 0x0; + // 4. GPIO Port COntrol (GPIOPCTL) + GPIOD -> PCTL = 0x0; + GPIOE -> PCTL = 0x0; + GPIOF -> PCTL = 0x00005750; + // 5. Clear AFSEL bits to 0 to select regular I/O + GPIOD -> AFSEL = 0x0; + GPIOE -> AFSEL = 0x0; + GPIOF -> AFSEL |= _PIN1|_PIN2|_PIN3; + // 6. GPIO Pin Direction (GPIODIR) 0 for input, 1 for output + GPIOD -> DIR |= _PIN0|_PIN1|_PIN2|_PIN3; + GPIOE -> DIR |= _PIN1|_PIN2|_PIN3; + GPIOF -> DIR |= _PIN1|_PIN3; + // 7. Set PUR bits to 1 to enable internal pull-up resistor + // 8. Set DEN bits to 1 to enable data pins + GPIOD -> DEN |= _PIN0|_PIN1|_PIN2|_PIN3; + GPIOE -> DEN |= _PIN1|_PIN2|_PIN3; + GPIOF -> DEN |= _PIN1|_PIN2|_PIN3; + +} + +void Setup_PWM(){ + //PF1 M1PWM5 GEN 2 + //PF3 M1PWM7 GEN 3 + int load = 8; + // 1. Enable Clock for PWM Module 1 + SYSCTL->RCGCPWM |= _PWM_MODULE1; + while((SYSCTL->PRPWM & _PWM_MODULE1)!= _PWM_MODULE1){}; + // 2. Enable and Setup Clock Divider for PWM + SYSCTL->RCC |= (0 << 20); // RCC[20]=1:USEPWMDIV + //SYSCTL->RCC &= ~0x000E0000; // RCC[19:17]=000 PWMDIV + //SYSCTL->RCC |= (_PWMDIV_2 << 17); // RCC[19:17]=0x04 divider=/32 + // 3. Disable PWM Generator 2 + PWM1->_2_CTL &= ~0x01; // Disable PWM Generator 3 + PWM1->_3_CTL &= ~0x01; // Disable PWM Generator 3 + // 4. Config LOAD, CMPn, GENn values + PWM1->_2_LOAD = load; // GEN 2 B PWM 5 PF1 LED + PWM1->_2_CMPB = load/2; + PWM1->_2_GENB = _PWM_RIGHT_ALIG_CMPBD;//0x080C + + PWM1->_3_LOAD = load; // GEN 3 B PWM 7 PF3 PWM OUT + PWM1->_3_CMPB = load/2; + PWM1->_3_GENB = _PWM_RIGHT_ALIG_CMPBD;//0x080C + // 5. Enable PWM Generator 2 + PWM1->_3_CTL |= 0x01; + PWM1->_3_CTL |= 0x01; + // 6. Enable PWM5 Output + PWM1 -> ENABLE |= _PWM5; + PWM1 -> ENABLE |= _PWM7; + + +} \ No newline at end of file diff --git a/measureFreqDutyLCD.c b/measureFreqDutyLCD.c new file mode 100755 index 0000000..a34d0f7 --- /dev/null +++ b/measureFreqDutyLCD.c @@ -0,0 +1,269 @@ +#include +#include +#include +#include + +#include // The Header File for EK-TM4C123GXL LaunchPad +// #include // The Header File for EK-TM4C1294XL LaunchPad +#include "ez123G.h" +#include "MyDefines.h" // Your Definitions for the System Configuration + +#define _TIMER_CFG_1632B_TIMER16B (0x4) +#define _TIMERA_CAPTURE_EVENT_INT_CLR (1 << 2) +#define _TIMERA_CAPTURE_EVENT_INT (1 << 2) +#define _TIMERB_CAPTURE_EVENT_INT_CLR (1 << 10) +#define _TIMERB_CAPTURE_EVENT_INT (1 << 10) + +float SYSTEM_CLOCK_FREQ = ________; + +typedef enum EDGE_EVENT{ + FALLING_EDGE, + RISING_EDGE + +} EDGE_EVENT; + +void Setup_PWM(void); +void Setup_Timer(void); +void Setup_GPIO(void); +void Setup_UART(void); + +uint32_t MeasurePeriod(void); +uint32_t MeasurePulseWidth(void); +void UART_PrintString(char * s); + +typedef struct PWM_CONTROL{ + uint16_t load; + uint16_t cmp; +} PWM_CONTROL; + +PWM_CONTROL Pwm[]={ + {18150, 4628}, + {6665, 3032}, + {12490, 8118}, + {49996, 37497}, + {8328, 2914} +}; + +// On-board switch +uint32_t *Sw = (uint32_t *) (((char*)_____)+ (______ << 2)); + +int main() +{ + PEZOBJ_LCD lcd; + volatile uint32_t T; + volatile uint32_t t; + uint32_t freq; + double duty; + char str[100]; + int idx = 0; + uint8_t i = 0; + bool preSw = true; + uint16_t currSw; + + Setup_123G_80MHz(); // Setup System Clock to 80 MHz + + Setup_PWM(); + Setup_Timer(); + Setup_GPIO(); + + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIO____, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIO____, PIN7); + ezLCD_Connect_RSPin(lcd, GPIO____, PIN6); + ezLCD_Connect_RWPin(lcd, GPIO____, PIN4); + + ezLCD_Start(lcd); + ezLCD_ClearDisplay(lcd); + + PWM0->_1_CTL &= ~0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL &= ~0x01; // Disable PWM Module 1, Generator 3 + PWM0->_1_LOAD = Pwm[idx].load; + PWM0->_1_CMPB = Pwm[idx].cmp; + PWM1->_3_LOAD = Pwm[idx].load; + PWM1->_3_CMPB = Pwm[idx].cmp; + PWM0->_1_CTL |= 0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL |= 0x01; // Disable PWM Module 1, Generator 3 + + while(1){ + + currSw = *Sw; + if (preSw && (!currSw)){ + if (++idx >= (sizeof(Pwm) / sizeof(PWM_CONTROL))) idx = 0; + PWM0->_1_CTL &= ~0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL &= ~0x01; // Disable PWM Module 1, Generator 3 + PWM0->_1_LOAD = Pwm[idx].load; + PWM0->_1_CMPB = Pwm[idx].cmp; + PWM1->_3_LOAD = Pwm[idx].load; + PWM1->_3_CMPB = Pwm[idx].cmp; + PWM0->_1_CTL |= 0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL |= 0x01; // Disable PWM Module 1, Generator 3 + timer_waitMillis(100); + } + preSw = currSw; + + T = MeasurePeriod(); + t = MeasurePulseWidth(); + freq = ____________ ; + duty = ____________ ; + sprintf(str, "%02d: f%d = %d Hz ", i, idx, freq); + ezLCD_Position(lcd, 0,0); + ezLCD_PrintString(lcd, str); + sprintf(str, "Duty = %.1f %% ", duty); + ezLCD_Position(lcd, 1,0); + ezLCD_PrintString(lcd, str); + i = (++i) % 100; + timer_waitMillis(500); + } + +} + +void Setup_PWM(void) +{ +ENB + + // 1. Enable Clock for PWM Module + SYSCTL->RCGCPWM |= (_______); + while((SYSCTL->PRPWM & (_______)) != (_______)){}; + // 2. Enable and Setup Clock Divider for PWM + SYSCTL->RCC |= (1 << 20); // RCC[20]=1:USEPWMDIV + SYSCTL->RCC &= ~(0x07 << 17); // RCC[19:17]=000 PWMDIV + SYSCTL->RCC |= (________ << 17); // RCC[19:17]=0x04 divider=/32 + // 3. Disable PWM Generator + // 4. Configure LOAD (Period), CMP (Initial Duty), GEN (PWM Mode) values + // 5. Enable PWM Generator + // 6. Enable PWM Output + +} +void Setup_Timer(void) +{ + // 1 . Enable Clock for TIMER + SYSCTL->RCGCTIMER |= ________; + while ((SYSCTL->PRTIMER & (________)) != (________)) {} + // 2. Disable TIMER + TIMER_->CTL &= ~(________); + // 3. Configure TIMER + TIMER_->CFG = _TIMER_CFG_1632B_TIMER16B; + // 4. Configure Timer n Mode: GPTMTAMR + TIMER_->TAMR = ________; + // 5. Configure Timer Event Mode + TIMER_->CTL &= ~________; + TIMER_->CTL |= ________; + // 6. Configure Load + TIMER_->TAILR = 0xffff; //0x00000000; + //Set the prescaler to 0xff + TIMER_->TAPR = 0xff; + TIMER_->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + TIMER_->IMR = 0; + // 7. Enable GPTM Timer + TIMER_->CTL |= ________; // Enable Timer + +} +void Setup_GPIO(void) +{ + + // GPIO Initialization and Configuration + // 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO) + SYSCTL->RCGCGPIO |= (_______); + // allow time for clock to stabilize (SYSCTL->PRGPIO) + while((SYSCTL->PRGPIO & (_______)) != (_______)){}; + // 2. Unlock PD7 and/or PF0 on TM4C123G (GPIO->LOCK and GPIO->CR) + //GPIOF->LOCK = 0x4C4F434B; + //GPIOF->CR |= _PIN0; + + //GPIOD_AHB->LOCK = 0x4C4F434B; + //while( (GPIOD_AHB->LOCK & 0x01) == 0x01) {}; + //*(((char*)GPIOD_AHB)+0x524) = 0xFF; + + // 3. GPIO Analog Mode Select (GPIOAMSEL) + // 4. GPIO Port COntrol (GPIOPCTL) + // 5. Clear AFSEL bits to 0 to select regular I/O + // 6. GPIO Pin Direction (GPIODIR) 0 for input, 1 for output + // 7. Set PUR bits to 1 to enable internal pull-up resistor + // 8. Set DEN bits to 1 to enable data pins + +} + +uint32_t MeasurePeriod(void) +{ + volatile uint32_t edge1; + volatile uint32_t edge2; + uint32_t load; + + // **** Capture firstEgde i.e. rising edge **** + TIMER_->CTL &= ~(_TIMERA_ENABLE); + TIMER_->CTL &= ~(_TIMERA_EVENT); + TIMER_->CTL |= ________; // Capture on the falling- or rising- edgs + TIMER_->CTL |= _TIMERA_ENABLE; + + //1. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register (CnERIS bit) + TIMER_->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + //2. Waiting for capture event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER_->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + //3. Read the capture value from GPTMTAR (TIMER1->TAR) register + edge1 = TIMER_->TAR; + + // **** Capture secondEgde i.e. falling edge **** + //4. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER_->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + + //5. Waiting for capture falling edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER_->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + + //6. Read the capture value from GPTMTAR (TIMERa->TAR) register + edge2 = TIMER_->TAR; + + //7. Calculate deltaT = highEdge - lowEdge + // Note: the deltaT must large than zero, cannot be negative value + + + + +} +uint32_t MeasurePulseWidth(void) +{ + uint32_t edge1; + uint32_t edge2; + static uint32_t load; + + // **** Capture first Rising-Edge **** + TIMER_->CTL &= ~(_TIMERA_ENABLE); + TIMER_->CTL &= ~(_TIMERA_EVENT); + TIMER_->CTL |= ________; // Capture on the Rising-Edge + TIMER_->CTL |= _TIMERA_ENABLE; + + //1. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER_->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + //2. Waiting for capture event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER_->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + //3. Read the capture value from GPTMTAR (TIMER1->TAR) register + edge1 = TIMER_->TAR; + + TIMER_->CTL &= ~(_TIMERA_EVENT); + TIMER_->CTL |= ________; // Capture on the Falling-Edge + + + // **** Capture second Egde i.e. falling edge **** + //4. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER_->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + + //5. Waiting for capture falling edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER_->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + + //6. Read the capture value from GPTMTAR (TIMERa->TAR) register + edge2 = TIMER_->TAR; + + //7. Calculate deltaT = highEdge - lowEdge + // Note: the deltaT must large than zero, cannot be negative value + + + +} + diff --git a/pwmanalyserfirstdraft.c b/pwmanalyserfirstdraft.c new file mode 100755 index 0000000..b51f418 --- /dev/null +++ b/pwmanalyserfirstdraft.c @@ -0,0 +1,297 @@ +#include +#include +#include +#include + +#include // The Header File for EK-TM4C123GXL LaunchPad +// #include // The Header File for EK-TM4C1294XL LaunchPad +#include "ez123G.h" +#include "MyDefines.h" // Your Definitions for the System Configuration + +#define _TIMER_CFG_1632B_TIMER16B (0x4) +#define _TIMERA_CAPTURE_EVENT_INT_CLR (1 << 2) +#define _TIMERA_CAPTURE_EVENT_INT (1 << 2) +#define _TIMERB_CAPTURE_EVENT_INT_CLR (1 << 10) +#define _TIMERB_CAPTURE_EVENT_INT (1 << 10) + +float SYSTEM_CLOCK_FREQ = 80E6; + +typedef enum EDGE_EVENT{ + FALLING_EDGE, + RISING_EDGE + +} EDGE_EVENT; + +void Setup_PWM(void); +void Setup_Timer(void); +void Setup_GPIO(void); +void Setup_UART(void); + +uint32_t MeasurePeriod(void); +uint32_t MeasurePulseWidth(void); +void UART_PrintString(char * s); + +typedef struct PWM_CONTROL{ + uint16_t load; + uint16_t cmp; +} PWM_CONTROL; + +PWM_CONTROL Pwm[]={ + {18150, 4628}, + {6665, 3032}, + {12490, 8118}, + {49996, 37497}, + {8328, 2914} +}; + +// On-board switch +uint32_t *Sw = (uint32_t *) (((char*)GPIOF)+ (_PIN4 << 2)); + +int main() +{ + PEZOBJ_LCD lcd; + volatile uint32_t T; + volatile uint32_t t; + uint32_t freq; + double duty; + char str[100]; + int idx = 0; + uint8_t i = 0; + bool preSw = true; + uint16_t currSw; + + Setup_123G_80MHz(); // Setup System Clock to 80 MHz + + Setup_PWM(); + Setup_Timer(); + Setup_GPIO(); + + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIOE, PIN1); + ezLCD_Connect_RSPin(lcd, GPIOE, PIN2); + ezLCD_Connect_RWPin(lcd, GPIOE, PIN3); + + ezLCD_Start(lcd); + ezLCD_ClearDisplay(lcd); + + PWM0->_1_CTL &= ~0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL &= ~0x01; // Disable PWM Module 1, Generator 3 + PWM0->_1_LOAD = Pwm[idx].load; + PWM0->_1_CMPB = Pwm[idx].cmp; + PWM1->_3_LOAD = Pwm[idx].load; + PWM1->_3_CMPB = Pwm[idx].cmp; + PWM0->_1_CTL |= 0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL |= 0x01; // Disable PWM Module 1, Generator 3 + + while(1){ + + currSw = *Sw; + if (preSw && (!currSw)){ + if (++idx >= (sizeof(Pwm) / sizeof(PWM_CONTROL))) idx = 0; + PWM0->_1_CTL &= ~0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL &= ~0x01; // Disable PWM Module 1, Generator 3 + PWM0->_1_LOAD = Pwm[idx].load; + PWM0->_1_CMPB = Pwm[idx].cmp; + PWM1->_3_LOAD = Pwm[idx].load; + PWM1->_3_CMPB = Pwm[idx].cmp; + PWM0->_1_CTL |= 0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL |= 0x01; // Disable PWM Module 1, Generator 3 + timer_waitMillis(100); + } + preSw = currSw; + + T = MeasurePeriod(); + t = MeasurePulseWidth(); + freq = 1 / (T * (1.25E-8)); + duty = (t * 100) / T ; + sprintf(str, "%02d: f%d = %d Hz ", i, idx, freq); + ezLCD_Position(lcd, 0,0); + ezLCD_PrintString(lcd, str); + sprintf(str, "Duty = %.1f %% ", duty); + ezLCD_Position(lcd, 1,0); + ezLCD_PrintString(lcd, str); + i = (++i) % 100; + timer_waitMillis(500); + } + +} + +void Setup_PWM(void) +{ + //PF1 M1PWM5 GEN 2 + //PF3 M1PWM7 GEN 3 + int load = 8; + // 1. Enable Clock for PWM Module 1 + SYSCTL->RCGCPWM |= _PWM_MODULE1; + while((SYSCTL->PRPWM & _PWM_MODULE1)!= _PWM_MODULE1){}; + // 2. Enable and Setup Clock Divider for PWM + SYSCTL->RCC |= (0 << 20); // RCC[20]=1:USEPWMDIV + //SYSCTL->RCC &= ~0x000E0000; // RCC[19:17]=000 PWMDIV + //SYSCTL->RCC |= (_PWMDIV_2 << 17); // RCC[19:17]=0x04 divider=/32 + // 3. Disable PWM Generator 2 + PWM1->_2_CTL &= ~0x01; // Disable PWM Generator 3 + PWM1->_3_CTL &= ~0x01; // Disable PWM Generator 3 + // 4. Config LOAD, CMPn, GENn values + PWM1->_2_LOAD = load; // GEN 2 B PWM 5 PF1 LED + PWM1->_2_CMPB = load/2; + PWM1->_2_GENB = _PWM_RIGHT_ALIG_CMPBD;//0x080C + + PWM1->_3_LOAD = load; // GEN 3 B PWM 7 PF3 PWM OUT + PWM1->_3_CMPB = load/2; + PWM1->_3_GENB = _PWM_RIGHT_ALIG_CMPBD;//0x080C + // 5. Enable PWM Generator 2 + PWM1->_3_CTL |= 0x01; + PWM1->_3_CTL |= 0x01; + // 6. Enable PWM5 Output + PWM1 -> ENABLE |= _PWM5; + PWM1 -> ENABLE |= _PWM7; + +} +void Setup_Timer(void) +{ + // 1 . Enable Clock for TIMER + SYSCTL->RCGCTIMER |= _TIMER1; + while ((SYSCTL->PRTIMER & (_TIMER1)) != (_TIMER1)) {} + // 2. Disable TIMER + TIMER1->CTL &= ~(_TIMERA_ENABLE); + // 3. Configure TIMER + TIMER1->CFG = _TIMER_CFG_1632B_TIMER16B; + // 4. Configure Timer n Mode: GPTMTAMR + TIMER1->TAMR = (_TIMERA_COUNTDOWN|_TIMERA_EDGE_TIME|_TIMERA_CAPTURE); + // 5. Configure Timer Event Mode + TIMER1->CTL &= ~(0x03 << 2); + TIMER1->CTL |= _TIMERA_BOTH_EDGES; + // 6. Configure Load + TIMER1->TAILR = 0xffff; //0x00000000; + //Set the prescaler to 0xff + TIMER1->TAPR = 0xff; + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + TIMER1->IMR = 0; + // 7. Enable GPTM Timer + TIMER1->CTL |= _TIMERA_ENABLE; // Enable Timer + +} +void Setup_GPIO(void) +{ + + // GPIO Initialization and Configuration + // 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO) + SYSCTL->RCGCGPIO |= _PORTD|_PORTE|_PORTF; + // allow time for clock to stabilize (SYSCTL->PRGPIO) + while((SYSCTL->PRGPIO & (_PORTD|_PORTE|_PORTF)) != (_PORTD|_PORTE|_PORTF)){}; + // 2. Unlock PD7 and PF0 on TM4C123G; or PD7 on TM4C1294 (GPIO->LOCK and GPIO->CR) + // 3. GPIO Analog Mode Select (GPIOAMSEL) + GPIOD -> AMSEL = 0x0; + GPIOE -> AMSEL = 0x0; + GPIOF -> AMSEL = 0x0; + // 4. GPIO Port Control (GPIOPCTL) + GPIOD -> PCTL = 0x0; + GPIOE -> PCTL = 0x0; + GPIOF -> PCTL = 0x00005750; + // 5. Clear AFSEL bits to 0 to select regular I/O + GPIOD -> AFSEL = 0x0; + GPIOE -> AFSEL = 0x0; + GPIOF -> AFSEL |= _PIN1|_PIN2|_PIN3; + // 6. GPIO Pin Direction (GPIODIR) 0 for input, 1 for output + GPIOD -> DIR |= _PIN0|_PIN1|_PIN2|_PIN3; + GPIOE -> DIR |= _PIN1|_PIN2|_PIN3; + GPIOF -> DIR |= _PIN1|_PIN3; + // 7. Set PUR bits to 1 to enable internal pull-up resistor + GPIOF -> PUR |= _PIN4; + // 8. Set DEN bits to 1 to enable data pins + GPIOD -> DEN |= _PIN0|_PIN1|_PIN2|_PIN3; + GPIOE -> DEN |= _PIN1|_PIN2|_PIN3; + GPIOF -> DEN |= _PIN1|_PIN2|_PIN3|_PIN4; +} + +uint32_t MeasurePeriod(void) +{ + volatile uint32_t edge1; + volatile uint32_t edge2; + uint32_t load; + + // **** Capture firstEgde i.e. rising edge **** + TIMER1->CTL &= ~(_TIMERA_ENABLE); + TIMER1->CTL &= ~(0x03); + TIMER1->CTL |= _TIMERA_POSITIVE_EDGE; // Capture on the falling- or rising- edgs + TIMER1->CTL |= _TIMERA_ENABLE; + + //1. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register (CnERIS bit) + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + //2. Waiting for capture event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER1->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + //3. Read the capture value from GPTMTAR (TIMER1->TAR) register + edge1 = TIMER1->TAR; + + // **** Capture secondEgde i.e. falling edge **** + //4. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + + //5. Waiting for capture falling edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER1->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + + //6. Read the capture value from GPTMTAR (TIMERa->TAR) register + edge2 = TIMER1->TAR; + + //7. Calculate deltaT = highEdge - lowEdge + // Note: the deltaT must large than zero, cannot be negative value + if(edge1 > edge2){ + return edge1 - edge2; + }else{ + return (0xFFFFFFFF - (edge2 - edge1)); + } +} + +uint32_t MeasurePulseWidth(void) +{ + uint32_t edge1; + uint32_t edge2; + static uint32_t load; + + // **** Capture first Rising-Edge **** + TIMER1->CTL &= ~(_TIMERA_ENABLE); + TIMER1->CTL &= ~(0x03); + TIMER1->CTL |= _TIMERA_POSITIVE_EDGE; // Capture on the Rising-Edge + TIMER1->CTL |= _TIMERA_ENABLE; + + //1. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + //2. Waiting for capture event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER1->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + //3. Read the capture value from GPTMTAR (TIMER1->TAR) register + edge1 = TIMER1->TAR; + + TIMER1->CTL &= ~(0x03); + TIMER1->CTL |= _TIMERA_NEGATIVE_EDGE; // Capture on the Falling-Edge + + + // **** Capture second Egde i.e. falling edge **** + //4. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + + //5. Waiting for capture falling edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER1->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + + //6. Read the capture value from GPTMTAR (TIMERa->TAR) register + edge2 = TIMER1->TAR; + + //7. Calculate deltaT = highEdge - lowEdge + // Note: the deltaT must large than zero, cannot be negative value + if(edge1 > edge2){ + return edge1 - edge2; + }else{ + return (0xFFFFFFFF - (edge2 - edge1)); + } + + +} + diff --git a/pwmmeasurer/MyDefines.h b/pwmmeasurer/MyDefines.h new file mode 100755 index 0000000..146c609 --- /dev/null +++ b/pwmmeasurer/MyDefines.h @@ -0,0 +1,65 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#define _PWM_RIGHT_ALIG_CMPAD ((0x03 << 6) | (0x02 << 2)) +#define _PWM_RIGHT_ALIG_CMPBD ((0x03 << 10) | (0x02 << 2)) + +#define _TIMER0 0x01 +#define _TIMER1 0x02 +#define _TIMER2 0x04 +#define _TIMER3 0x08 +#define _TIMER4 0x10 +#define _TIMER5 0x20 +#define _TIMER6 0x40 +#define _TIMER7 0x80 + +#define _TIMERA_ENABLE (1 << 0) +#define _TIMERB_ENABLE (1 << 8) +#define _TIMERA_COUNTUP (1 << 4) +#define _TIMERA_COUNTDOWN (0 << 4) +#define _TIMERA_CAPTURE 0x03 +#define _TIMERA_EDGE_TIME (1 << 2) +#define _TIMERA_POSITIVE_EDGE 0 +#define _TIMERA_NEGATIVE_EDGE (1 << 2) +#define _TIMERA_BOTH_EDGES (3 << 2) + +#endif \ No newline at end of file diff --git a/pwmmeasurer/main.c b/pwmmeasurer/main.c new file mode 100755 index 0000000..fe78b98 --- /dev/null +++ b/pwmmeasurer/main.c @@ -0,0 +1,314 @@ +#include +#include +#include +#include + +#include "TM4C123GH6PM.h" // The Header File for EK-TM4C123GXL LaunchPad +#include "ez123G.h" +#include "MyDefines.h" // Your Definitions for the System Configuration + +#define _TIMER_CFG_1632B_TIMER16B (0x4) +#define _TIMERA_CAPTURE_EVENT_INT_CLR (1 << 2) +#define _TIMERA_CAPTURE_EVENT_INT (1 << 2) +#define _TIMERB_CAPTURE_EVENT_INT_CLR (1 << 10) +#define _TIMERB_CAPTURE_EVENT_INT (1 << 10) + +float SYSTEM_CLOCK_FREQ = 80E6; + +typedef enum EDGE_EVENT{ + FALLING_EDGE, + RISING_EDGE + +} EDGE_EVENT; + +void Setup_PWM(void); +void Setup_Timer(void); +void Setup_GPIO(void); + +uint32_t MeasurePeriod(void); +uint32_t MeasurePulseWidth(void); + +typedef struct PWM_CONTROL{ + uint16_t load; + uint16_t cmp; +} PWM_CONTROL; + +PWM_CONTROL Pwm[]={ + {18150, 4628}, + {6665, 3032}, + {12490, 8118}, + {49996, 37497}, + {8328, 2914} +}; + +// On-board switch +uint32_t *Sw = (uint32_t *) (((char*)GPIOF)+ (_PIN4 << 2)); + +int main() +{ + PEZOBJ_LCD lcd; + volatile uint32_t T; + volatile uint32_t t; + uint32_t freq; + double duty; + char str[100]; + int idx = 0; + uint8_t i = 0; + bool preSw = true; + uint16_t currSw; + + Setup_123G_80MHz(); // Setup System Clock to 80 MHz + + Setup_PWM(); + Setup_Timer(); + Setup_GPIO(); + + lcd = ezLCD_Create(); + ezLCD_Connect_DataPort(lcd, GPIOD, PIN_3_0); + ezLCD_Connect_ENPin(lcd, GPIOE, PIN1); + ezLCD_Connect_RSPin(lcd, GPIOE, PIN2); + ezLCD_Connect_RWPin(lcd, GPIOE, PIN3); + + ezLCD_Start(lcd); + ezLCD_LoadVerticalBargraphFonts(lcd); + ezLCD_ClearDisplay(lcd); + //ezLCD_PrintString(lcd, "HELLO WORLD"); + + PWM0->_1_CTL &= ~0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL &= ~0x01; // Disable PWM Module 1, Generator 3 + PWM0->_1_LOAD = Pwm[idx].load; + PWM0->_1_CMPB = Pwm[idx].cmp; + PWM1->_3_LOAD = Pwm[idx].load; + PWM1->_3_CMPB = Pwm[idx].cmp; + PWM0->_1_CTL |= 0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL |= 0x01; // Disable PWM Module 1, Generator 3 + + while(1){ + + currSw = *Sw; + if (preSw && (!currSw)){ + if (++idx >= (sizeof(Pwm) / sizeof(PWM_CONTROL))) idx = 0; + PWM0->_1_CTL &= ~0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL &= ~0x01; // Disable PWM Module 1, Generator 3 + PWM0->_1_LOAD = Pwm[idx].load; + PWM0->_1_CMPB = Pwm[idx].cmp; + PWM1->_3_LOAD = Pwm[idx].load; + PWM1->_3_CMPB = Pwm[idx].cmp; + PWM0->_1_CTL |= 0x01; // Disable PWM Module 0, Generator 1 + PWM1->_3_CTL |= 0x01; // Disable PWM Module 1, Generator 3 + timer_waitMillis(100); + } + preSw = currSw; + + T = MeasurePeriod(); + t = MeasurePulseWidth(); + freq = 1 / (T * (1.25E-8)); + duty = (t * 100)/T ; + sprintf(str, "%02d: f%d = %d Hz ", i, idx, freq); + ezLCD_Position(lcd, 0,0); + ezLCD_PrintString(lcd, str); + sprintf(str, "Duty = %.1f %% ", duty); + ezLCD_Position(lcd, 1,0); + ezLCD_PrintString(lcd, str); + i = (++i) % 100; + timer_waitMillis(500); + } + +} + +void Setup_PWM(void) +{ + // M0 GEN 1 B PWM 3 PB5 PWM + // M1 GEN 3 B PWM 7 PF3 LED + + // 1. Enable Clock for PWM Module + SYSCTL->RCGCPWM |= (_PWM_MODULE0|_PWM_MODULE1); + while((SYSCTL->PRPWM & (_PWM_MODULE0|_PWM_MODULE1)) != (_PWM_MODULE0|_PWM_MODULE1)){}; + // 2. Enable and Setup Clock Divider for PWM + SYSCTL->RCC |= (1 << 20); // RCC[20]=1:USEPWMDIV + SYSCTL->RCC &= ~(0x07 << 17); // RCC[19:17]=000 PWMDIV + SYSCTL->RCC |= (_PWMDIV_8 << 17); // RCC[19:17]=0x04 divider=/32 + // 3. Disable PWM Generator + PWM0->_1_CTL &= ~0x01; // Disable PWM Generator 1 + PWM1->_3_CTL &= ~0x01; // Disable PWM Generator 3 + // 4. Configure LOAD (Period), CMP (Initial Duty), GEN (PWM Mode) values + PWM0->_1_LOAD = 18150; + PWM0->_1_CMPB = 4628; + PWM1->_3_LOAD = 18150; + PWM1->_3_CMPB = 4628; + + PWM0 -> _1_GENB = _PWM_RIGHT_ALIG_CMPBD; + PWM1 -> _3_GENB = _PWM_RIGHT_ALIG_CMPBD; + // 5. Enable PWM Generator + PWM0->_1_CTL |= 0x01; + PWM1->_3_CTL |= 0x01; + // 6. Enable PWM Output + PWM0 -> ENABLE = _PWM3; + PWM1 -> ENABLE = _PWM7; + + +} +void Setup_Timer(void) +{ + //PF2 T1CCP0 + + // 1 . Enable Clock for TIMER + SYSCTL->RCGCTIMER |= _TIMER1; + while ((SYSCTL->PRTIMER & (_TIMER1)) != (_TIMER1)) {} + // 2. Disable TIMER + TIMER1->CTL &= ~(_TIMERA_ENABLE); + // 3. Configure TIMER + TIMER1->CFG = _TIMER_CFG_1632B_TIMER16B; + // 4. Configure Timer n Mode: GPTMTAMR + TIMER1->TAMR = (_TIMERA_COUNTDOWN|_TIMERA_EDGE_TIME|_TIMERA_CAPTURE); + // 5. Configure Timer Event Mode + TIMER1->CTL &= ~(0x03 << 2); + TIMER1->CTL |= _TIMERA_BOTH_EDGES; + // 6. Configure Load + TIMER1->TAILR = 0xffff; //0x00000000; + //Set the prescaler to 0xff + TIMER1->TAPR = 0xff; + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + TIMER1->IMR = 0; + // 7. Enable GPTM Timer + TIMER1->CTL |= _TIMERA_ENABLE; // Enable Timer + +} +void Setup_GPIO(void) +{ + + // GPIO Initialization and Configuration + // 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO) + SYSCTL->RCGCGPIO |= (_PORTB|_PORTD|_PORTE|_PORTF); + // allow time for clock to stabilize (SYSCTL->PRGPIO) + while((SYSCTL->PRGPIO & (_PORTB|_PORTD|_PORTE|_PORTF)) != (_PORTB|_PORTD|_PORTE|_PORTF)){}; + // 2. Unlock PD7 and/or PF0 on TM4C123G (GPIO->LOCK and GPIO->CR) + //GPIOF->LOCK = 0x4C4F434B; + //GPIOF->CR |= _PIN0; + + //GPIOD_AHB->LOCK = 0x4C4F434B; + //while( (GPIOD_AHB->LOCK & 0x01) == 0x01) {}; + //*(((char*)GPIOD_AHB)+0x524) = 0xFF; + + // 3. GPIO Analog Mode Select (GPIOAMSEL) + GPIOB -> AMSEL = 0x00; + GPIOD -> AMSEL = 0x00; + GPIOE -> AMSEL = 0x00; + GPIOF -> AMSEL = 0x00; + // 4. GPIO Port COntrol (GPIOPCTL) + GPIOB -> PCTL = 0x00400000; + GPIOD -> PCTL = 0x00; + GPIOE -> PCTL = 0x00; + GPIOF -> PCTL = 0x00005700; + // 5. Clear AFSEL bits to 0 to select regular I/O + GPIOB -> AFSEL |= _PIN5; + GPIOD -> AFSEL = 0x0; + GPIOE -> AFSEL = 0x0; + GPIOF -> AFSEL |= (_PIN2|_PIN3); + // 6. GPIO Pin Direction (GPIODIR) 0 for input, 1 for output + GPIOB -> DIR |= (_PIN5); + GPIOD -> DIR |= (_PIN0|_PIN1|_PIN2|_PIN3); + GPIOE -> DIR |= (_PIN1|_PIN2|_PIN3); + GPIOF -> DIR |= (_PIN3); + // 7. Set PUR bits to 1 to enable internal pull-up resistor + GPIOF -> PUR |= (_PIN4); + // 8. Set DEN bits to 1 to enable data pins + GPIOB -> DEN |= (_PIN5); + GPIOD -> DEN |= (_PIN0|_PIN1|_PIN2|_PIN3); + GPIOE -> DEN |= (_PIN1|_PIN2|_PIN3); + GPIOF -> DEN |= (_PIN2|_PIN3|_PIN4); + +} + +uint32_t MeasurePeriod(void) +{ + volatile uint32_t edge1; + volatile uint32_t edge2; + uint32_t load; + + // **** Capture firstEgde i.e. rising edge **** + TIMER1->CTL &= ~(_TIMERA_ENABLE); + TIMER1->CTL &= ~(0x03 << 2); + TIMER1->CTL |= _TIMERA_POSITIVE_EDGE; // Capture on the falling- or rising- edgs + TIMER1->CTL |= _TIMERA_ENABLE; + + //1. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register (CnERIS bit) + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + //2. Waiting for capture event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER1->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + //3. Read the capture value from GPTMTAR (TIMER1->TAR) register + edge1 = TIMER1->TAR; + + // **** Capture secondEgde i.e. falling edge **** + //4. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + + //5. Waiting for capture falling edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER1->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + + //6. Read the capture value from GPTMTAR (TIMERa->TAR) register + edge2 = TIMER1->TAR; + + //7. Calculate deltaT = highEdge - lowEdge + // Note: the deltaT must large than zero, cannot be negative value + if(edge1 > edge2){ + return edge1 - edge2; + }else{ + return (0xFFFFFFFF - (edge2 - edge1)); + } + + + +} +uint32_t MeasurePulseWidth(void) +{ + uint32_t edge1; + uint32_t edge2; + static uint32_t load; + + // **** Capture first Rising-Edge **** + TIMER1->CTL &= ~(_TIMERA_ENABLE); + TIMER1->CTL &= ~(0x03 << 2); + TIMER1->CTL |= _TIMERA_POSITIVE_EDGE; // Capture on the Rising-Edge + TIMER1->CTL |= _TIMERA_ENABLE; + + //1. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + //2. Waiting for capture event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER1->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + //3. Read the capture value from GPTMTAR (TIMER1->TAR) register + edge1 = TIMER1->TAR; + + TIMER1->CTL &= ~(0x03 << 2); + TIMER1->CTL |= _TIMERA_NEGATIVE_EDGE; // Capture on the Falling-Edge + + + // **** Capture second Egde i.e. falling edge **** + //4. Clear GPTM Timer A Capture Mode Event by writing 1 to + // corresponding bit on GPTMICR (TIMER1->ICR) register + TIMER1->ICR = _TIMERA_CAPTURE_EVENT_INT_CLR; + + //5. Waiting for capture falling edge event by check the GPTM Raw Interrupt Status + // GPTMRIS (TIMER1->RIS) register + while ((TIMER1->RIS & _TIMERA_CAPTURE_EVENT_INT) != _TIMERA_CAPTURE_EVENT_INT) {}; + + //6. Read the capture value from GPTMTAR (TIMERa->TAR) register + edge2 = TIMER1->TAR; + + //7. Calculate deltaT = highEdge - lowEdge + // Note: the deltaT must large than zero, cannot be negative value + if(edge1 > edge2){ + return edge1 - edge2; + }else{ + return (0xFFFFFFFF - (edge2 - edge1)); + } + + +} + diff --git a/servo/MyDefines.h b/servo/MyDefines.h new file mode 100755 index 0000000..fb99053 --- /dev/null +++ b/servo/MyDefines.h @@ -0,0 +1,43 @@ +#ifndef __MYDEFINES_H +#define __MYDEFINES_H + +#define _PIN0 0x01 +#define _PIN1 0x02 +#define _PIN2 0x04 +#define _PIN3 0x08 +#define _PIN4 0x10 +#define _PIN5 0x20 +#define _PIN6 0x40 +#define _PIN7 0x80 + +#define _PORTA 0x0001 +#define _PORTB 0x0002 +#define _PORTC 0x0004 +#define _PORTD 0x0008 +#define _PORTE 0x0010 +#define _PORTF 0x0020 + + +#define _PWM_MODULE0 0x01 +#define _PWM_MODULE1 0x02 + +#define _PWM0 0x01 +#define _PWM1 0x02 +#define _PWM2 0x04 +#define _PWM3 0x08 +#define _PWM4 0x10 +#define _PWM5 0x20 +#define _PWM6 0x40 +#define _PWM7 0x80 + +#define _PWMDIV_2 0x0 +#define _PWMDIV_4 0x1 +#define _PWMDIV_8 0x2 +#define _PWMDIV_16 0x3 +#define _PWMDIV_32 0x4 +#define _PWMDIV_64 0x5 + +#define _PWM_LEFT_ALIG_CMPAD ((0x02 << 6) | (0x03 << 2)) +#define _PWM_LEFT_ALIG_CMPBD ((0x02 << 10) | (0x03 << 2)) + +#endif \ No newline at end of file diff --git a/servo/main.c b/servo/main.c new file mode 100755 index 0000000..8fc4a17 --- /dev/null +++ b/servo/main.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include "MyDefines.h" + +#include "TM4C123GH6PM.h" + +void SetupPWM(void); +void SetupGPIO(void); + +void Delay(int s) +{ + volatile int i, j; + for (i = 0; i < s; i++){ + for(j = 0; j < 3180; j++){}} +} + +#define CMP_0_5MS 19500 +#define CMP_1_5MS 18500 +#define CMP_2_5MS 17500 +#define TO_RIGHT 0 +int main() +{ + uint16_t cmp = CMP_1_5MS; + bool dir = TO_RIGHT; + SetupPWM(); + SetupGPIO(); + + while(1){ + if(dir == TO_RIGHT){ + cmp += 10; + if( cmp >= CMP_0_5MS){ + cmp = CMP_0_5MS; + dir = !dir; + } + }else{ + cmp -= 10; + if(cmp <= CMP_2_5MS){ + cmp = CMP_2_5MS; + dir = !dir; + } + } + PWM1->_3_CMPB = cmp; + Delay(10); + } +} + +void SetupGPIO(){ + // Config for GPIO + // 1. Enable Clock on GPIOF + SYSCTL->RCGCGPIO = _PORTF; + // allow time for clock to stabilize + while((SYSCTL->PRGPIO & _PORTF) != _PORTF){}; + // 2. Unlock GPIO + // 3. Clear AMSEL to disable analog + GPIOF -> AMSEL = 0x0; + // 4. Config PCTL to select GPIO + GPIOF -> PCTL = 0x00005000; + // 5. Set DIR to 0 for input, 1 for output + GPIOF -> DIR = _PIN3; + // 6. Enable AFSEL bits to 1 + GPIOF -> AFSEL = _PIN3; + // 7. Set PUE bits to 1 to enable internal pull-up + // 8. Set DEN bits to 1 to enable data pins + GPIOF -> DEN = _PIN3; +} + +void SetupPWM(void) +{ + // 1. Enable Clock for PWM Module 1 + SYSCTL->RCGCPWM |= _PWM_MODULE1; + while((SYSCTL->PRPWM & _PWM_MODULE1)==0){}; + // 2. Enable and Setup Clock Divider for PWM + SYSCTL->RCC |= (1 << 20); // RCC[20]=1:USEPWMDIV + SYSCTL->RCC &= ~0x000E0000; // RCC[19:17]=000 PWMDIV + SYSCTL->RCC |= (_PWMDIV_16 << 17); // RCC[19:17]=0x04 divider=/32 + // 3. Disable PWM Generator 2 + PWM1->_3_CTL &= ~0x01; // Disable PWM Generator 2 + // 4. Config LOAD, CMPn, GENn values + PWM1->_3_LOAD = 20000; // GEN 3 B PWM 7 PF3 MOTOR + PWM1->_3_CMPB = 18500; + PWM1->_3_GENB = (0x02 << 10 ) | (0x03 <<2);//0x080C + // 5. Enable PWM Generator 3 + PWM1->_3_CTL |= 0x01; + // 6. Enable PWM7 Output + PWM1 -> ENABLE |= _PWM7; +} \ No newline at end of file