/*
Header: // for #include (see below) and also add file to folder
PLL.h
SysTick.h
Files: // add into Microvision and Folder
PLL.c
SysTick.c
startup.s
*/
#include <stdint.h>
#include "PLL.h"
#include "SysTick.h"
// for volatile... values, go to bottom of the page for explanation
#define LIGHT (*((volatile unsigned long *)0x400050FC)) // base & 40005000 (PORTB)
#define PLIGHT (*((volatile unsigned long *)0x4002503C)) // base & 40025000 (PORTF)
#define GPIO_PORTB_OUT (*((volatile unsigned long *)0x400050FC)) // bits 5-0
#define GPIO_PORTF_OUT (*((volatile unsigned long *)0x40025028))
#define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_AMSEL_R (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4000552C))
#define GPIO_PORTE_IN (*((volatile unsigned long *)0x4002401C)) // bits 2-0
#define SENSOR (*((volatile unsigned long *)0x4002401C)) // &PE 4002400
#define GPIO_PORTE_DIR_R (*((volatile unsigned long *)0x40024400))
#define GPIO_PORTE_AFSEL_R (*((volatile unsigned long *)0x40024420))
#define GPIO_PORTE_DEN_R (*((volatile unsigned long *)0x4002451C))
#define GPIO_PORTE_AMSEL_R (*((volatile unsigned long *)0x40024528))
#define GPIO_PORTE_PCTL_R (*((volatile unsigned long *)0x4002452C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400)) // additional port, F
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C))
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025028))
#define SYSCTL_RCGC2_GPIOF 0x00000020 // port F Clock Gating Control
#define SYSCTL_RCGC2_GPIOE 0x00000010 // port E Clock Gating Control
#define SYSCTL_RCGC2_GPIOB 0x00000002 // port B Clock Gating Control
// Linked data structure
struct State {
unsigned long Out;
unsigned long Pout;
unsigned long Time;
unsigned long Next[8];
};
typedef const struct State STyp;
#define goS 0
#define CautionS 1
#define goW 2
#define CautionW 3
#define PwaitS 4
#define PgoS 5
#define PblinkONS1 6
#define PblinkOFFS1 7
#define PblinkONS2 8
#define PblinkOFFS2 9
#define PwaitW 10
#define PgoW 11
#define PblinkONW1 12
#define PblinkOFFW1 13
#define PblinkONW2 14
#define PblinkOFFW2 15
STyp FSM[16]={ // each line, different states
{0x21, 0x02, 550,{goS,CautionS,goS,CautionS,PwaitS,PwaitS,PwaitS,PwaitS}},
{0x22, 0x02, 550,{goW,goW,goW,goW,goW,goW,goW,goW}},
{0x0C, 0x02, 550,{goW,goW,CautionW,CautionW,PwaitW,PwaitW,PwaitW,PwaitW}},
{0x14, 0x02, 550,{goS,goS,goS,goS,goS,goS,goS,goS}},
{0x22, 0x02, 550,{PgoS,PgoS,PgoS,PgoS,PgoS,PgoS,PgoS}},
{0x24, 0x08, 350,{PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1}},
{0x24, 0x02, 50,{PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1}},
{0x24, 0x00, 50,{PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2}},
{0x24, 0x02, 50,{PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2}},
{0x24, 0x00, 550,{goW,goW,goW,goW,goW,goW,goW,goW}},
{0x14, 0x02, 550,{PgoW,PgoW,PgoW,PgoW,PgoW,PgoW,PgoW,PgoW}},
{0x24, 0x08, 350,{PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1}},
{0x24, 0x02, 50,{PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1}},
{0x24, 0x00, 50,{PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2}},
{0x24, 0x02, 50,{PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2}},
{0x24, 0x00, 250,{goS,goS,goS,goS,goS,goS,goS,goS}}};
unsigned long S; // index to the current state
unsigned long Input;
int main(void){
volatile unsigned long delay;
PLL_Init(); // 80 MHz, Program 10.1
SysTick_Init(); // Program 10.2
SYSCTL_RCGC2_R |= 0x32; // 1) For Port B, E & F
delay = SYSCTL_RCGC2_R; // 2) no need to unlock
GPIO_PORTE_AMSEL_R &=~ 0x07; // disable analog function on PE1-0
GPIO_PORTE_AFSEL_R &=~ 0x07; // regular function on PE1-0
GPIO_PORTE_DIR_R &=~ 0x07; // inputs on PE1-0
GPIO_PORTE_DEN_R |= 0x07; // enable digital on PE1-0
GPIO_PORTE_PCTL_R &=~ 0x00000FFF; // enable regular GPIO
GPIO_PORTB_AMSEL_R &=~ 0x3F; // disable analog function on PB5-0
GPIO_PORTB_AFSEL_R &=~ 0x3F; // regular function on PB5-0
GPIO_PORTB_DIR_R |= 0x3F; // outputs on PB5-0
GPIO_PORTB_DEN_R |= 0x3F; // enable digital on PB5-0
GPIO_PORTB_PCTL_R &=~ 0x00FFFFFF; // enable regular GPIO
GPIO_PORTF_AMSEL_R &=~ 0x0A;
GPIO_PORTF_AFSEL_R &=~ 0x0A;
GPIO_PORTF_DIR_R |= 0x0A;
GPIO_PORTF_DEN_R |= 0x0A;
GPIO_PORTF_PUR_R = 0x0A;
GPIO_PORTF_PCTL_R &=~ 0x0000F0F0;
S = goS;
while(1){
LIGHT = FSM[S].Out; // set traffic lights
PLIGHT = FSM[S].Pout; //set Pedestrian lights
SysTick_Wait10ms(FSM[S].Time);
Input = SENSOR; // read sensors
S = FSM[S].Next[Input];
}
}
Defining the value for #LIGHT:
(accessing bits 0-5, see left table)
Base: 0x4000 5000
offsets: 0080
0040
+ 0020
0010
0008
0004
_________________
0x 4000 50(8+4+2+1 = 15 => F) (8+4 = 12 => C) == 0x4000 50FC
https://drive.google.com/drive/u/0/folders/1TmxG8vcH76qDIACuWfLp_aC4nEm4OfSI
Friday, March 29, 2019
Simple Traffic Light Code For Microvision
/* Required files:
PLL.c // add to software & folder
SysTick.c // add to software & folder
PLL.h // only on folder
SysTick.h // only on folder
startup.s
*/
#include "PLL.h"
#include "SysTick.h"
#define LIGHT (*((volatile unsigned long *)0x400050FC))
#define GPIO_PORTB_OUT (*((volatile unsigned long *)0x400050FC)) // bits 5-0
#define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_AMSEL_R (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4000552C))
#define GPIO_PORTE_IN (*((volatile unsigned long *)0x4002400C)) // bits 1-0
#define SENSOR (*((volatile unsigned long *)0x4002400C))
#define GPIO_PORTE_DIR_R (*((volatile unsigned long *)0x40024400))
#define GPIO_PORTE_AFSEL_R (*((volatile unsigned long *)0x40024420))
#define GPIO_PORTE_DEN_R (*((volatile unsigned long *)0x4002451C))
#define GPIO_PORTE_AMSEL_R (*((volatile unsigned long *)0x40024528))
#define GPIO_PORTE_PCTL_R (*((volatile unsigned long *)0x4002452C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define SYSCTL_RCGC2_GPIOE 0x00000010 // port E Clock Gating Control
#define SYSCTL_RCGC2_GPIOB 0x00000002 // port B Clock Gating Control
// Linked data structure
struct State {
unsigned long Out;
unsigned long Time;
unsigned long Next[4];
};
typedef const struct State STyp;
#define goN 0
#define waitN 1
#define goE 2
#define waitE 3
STyp FSM[4] = {
{0x21,500,{goN,waitN,goN,waitN}},
{0x22, 200,{goE,goE,goE,goE}},
{0x0C,500,{goE,goE,waitE,waitE}},
{0x14, 200,{goN,goN,goN,goN}}
};
unsigned long S; // index to the current state
unsigned long Input;
int main(void){
volatile unsigned long delay;
PLL_Init(); // 80 MHz, Program 10.1
SysTick_Init(); // Program 10.2
SYSCTL_RCGC2_R |= 0x12; // 1) B E
delay = SYSCTL_RCGC2_R; // 2) no need to unlock
GPIO_PORTE_AMSEL_R &= ~0x03; // 3) disable analog function on PE1-0
GPIO_PORTE_AFSEL_R &= ~0x03; // 6) regular function on PE1-0
GPIO_PORTE_DIR_R &=~ 0x03; // 5) inputs on PE1-0
GPIO_PORTE_DEN_R |= 0x03; // 7) enable digital on PE1-0
GPIO_PORTE_PCTL_R &=~ 0x000000FF; // 4) enable regular GPIO
GPIO_PORTB_AMSEL_R &= ~0x3F; // 3) disable analog function on PB5-0
GPIO_PORTB_PCTL_R &= ~0x00FFFFFF; // 4) enable regular GPIO
GPIO_PORTB_DIR_R |= 0x3F; // 5) outputs on PB5-0
GPIO_PORTB_AFSEL_R &= ~0x3F; // 6) regular function on PB5-0
GPIO_PORTB_DEN_R |= 0x3F; // 7) enable digital on PB5-0
S = goN;
while(1){
LIGHT = FSM[S].Out; // set lights
SysTick_Wait10ms(FSM[S].Time);
Input = SENSOR; // read sensors
S = FSM[S].Next[Input];
}
}
PLL.c // add to software & folder
SysTick.c // add to software & folder
PLL.h // only on folder
SysTick.h // only on folder
startup.s
*/
#include "PLL.h"
#include "SysTick.h"
#define LIGHT (*((volatile unsigned long *)0x400050FC))
#define GPIO_PORTB_OUT (*((volatile unsigned long *)0x400050FC)) // bits 5-0
#define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_AMSEL_R (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4000552C))
#define GPIO_PORTE_IN (*((volatile unsigned long *)0x4002400C)) // bits 1-0
#define SENSOR (*((volatile unsigned long *)0x4002400C))
#define GPIO_PORTE_DIR_R (*((volatile unsigned long *)0x40024400))
#define GPIO_PORTE_AFSEL_R (*((volatile unsigned long *)0x40024420))
#define GPIO_PORTE_DEN_R (*((volatile unsigned long *)0x4002451C))
#define GPIO_PORTE_AMSEL_R (*((volatile unsigned long *)0x40024528))
#define GPIO_PORTE_PCTL_R (*((volatile unsigned long *)0x4002452C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define SYSCTL_RCGC2_GPIOE 0x00000010 // port E Clock Gating Control
#define SYSCTL_RCGC2_GPIOB 0x00000002 // port B Clock Gating Control
// Linked data structure
struct State {
unsigned long Out;
unsigned long Time;
unsigned long Next[4];
};
typedef const struct State STyp;
#define goN 0
#define waitN 1
#define goE 2
#define waitE 3
STyp FSM[4] = {
{0x21,500,{goN,waitN,goN,waitN}},
{0x22, 200,{goE,goE,goE,goE}},
{0x0C,500,{goE,goE,waitE,waitE}},
{0x14, 200,{goN,goN,goN,goN}}
};
unsigned long S; // index to the current state
unsigned long Input;
int main(void){
volatile unsigned long delay;
PLL_Init(); // 80 MHz, Program 10.1
SysTick_Init(); // Program 10.2
SYSCTL_RCGC2_R |= 0x12; // 1) B E
delay = SYSCTL_RCGC2_R; // 2) no need to unlock
GPIO_PORTE_AMSEL_R &= ~0x03; // 3) disable analog function on PE1-0
GPIO_PORTE_AFSEL_R &= ~0x03; // 6) regular function on PE1-0
GPIO_PORTE_DIR_R &=~ 0x03; // 5) inputs on PE1-0
GPIO_PORTE_DEN_R |= 0x03; // 7) enable digital on PE1-0
GPIO_PORTE_PCTL_R &=~ 0x000000FF; // 4) enable regular GPIO
GPIO_PORTB_AMSEL_R &= ~0x3F; // 3) disable analog function on PB5-0
GPIO_PORTB_PCTL_R &= ~0x00FFFFFF; // 4) enable regular GPIO
GPIO_PORTB_DIR_R |= 0x3F; // 5) outputs on PB5-0
GPIO_PORTB_AFSEL_R &= ~0x3F; // 6) regular function on PB5-0
GPIO_PORTB_DEN_R |= 0x3F; // 7) enable digital on PB5-0
S = goN;
while(1){
LIGHT = FSM[S].Out; // set lights
SysTick_Wait10ms(FSM[S].Time);
Input = SENSOR; // read sensors
S = FSM[S].Next[Input];
}
}
Use 1k Resistors
Thursday, March 28, 2019
7-Segment Display Code using Kiel/Microvision
https://outreach.phas.ubc.ca/2015/06/05/an-introduction-to-seven-segment-displays/
Materials:
- 1x 7-segment display ✓
- 15+ wires ✓
- 10+ 500 or 1k resistors ✓
- 1x breadboard ✓
- 1x TI board ✓
- 1x power supply + power supply adapter (Optional but recommended to use it in this lab. They come with the Arduino kit.)
Before wiring and writing program, you are required to research on following topics:
1. Find out different types of 7-segment displays.
2. Based on the type of your own 7-segment display, figure out how to wire it correctly.
You need to build skills on researching and learning unknown knowledge independently and efficiently. This benefits you for long term such as when you take senior design, work in company, or invent your own products, etc. Try your best to get as little help from instructor as possible. When you struggle but try very hard to solve the problem, and once you solve it, you can remember it. That's the best way to improve building and problem solving skills. If you spent lots of time (5+ hours) and still struggling, and then it's the time to ask for help.
Step 1: Research and find out the different types of 7-segment displays.
Question 1: How many types of 7-segment displays?
Question 2: What are the names of these types.
Question 3: What are their differences?
Caution: Different types of 7-segment displays require different ways to wire.
Step 2: Wire all necessary pins. Make sure to connect a 500 - 1k resistor to each LED to prevent LED from burning.
In this lab, build a down counter system that meets following design specifications & requirements:
Specifications:
- System operates by a 3.3V source.
- System has one ARM chip.
- Design has 1 7-segment display.
Requirements:
- While system is running, counter counts up.
- When system turns on, counter begins counting from 0.
- System's counts 0, 1, ... to 9 and then repeats.
Using SysTick Code on Keil/Micro-vision
// LAB 5 - 7Display Counter
#include "inc/tm4c123gh6pm.h"
//void delay(); //Activating delay function
void PLL_Init(void){
// 0) Use RCC2
SYSCTL_RCC2_R |= 0x80000000; // USERCC2 // 1) bypass PLL while initializing
SYSCTL_RCC2_R |= 0x00000800; // BYPASS2, PLL bypass // 2) select the crystal value and oscillator source
SYSCTL_RCC_R = (SYSCTL_RCC_R &~0x000007C0) // clear XTAL field, bits 10-6
+ 0x00000540; // 10101, configure for 16 MHz crystal
SYSCTL_RCC2_R &= ~0x00000070; // configure for main oscillator source // 3) activate PLL by clearing PWRDN
SYSCTL_RCC2_R &= ~0x00002000; // 4) set the desired system divider
SYSCTL_RCC2_R |= 0x40000000; // use 400 MHz PLL
SYSCTL_RCC2_R = (SYSCTL_RCC2_R&~ 0x1FC00000) // clear system clock divider
+ (4<<22); // configure for 80 MHz clock // 5) wait for the PLL to lock by polling PLLLRIS
while((SYSCTL_RIS_R&0x00000040) == 0){}; // wait for PLLRIS bit
SYSCTL_RCC2_R &= ~0x00000800; // 6) enable use of PLL by clearing BYPASS
}
void SysTick_Init(void){
NVIC_ST_CTRL_R = 0; // disable SysTick during setup
NVIC_ST_CTRL_R = 0x00000005; // enable SysTick with core clock
}
// The delay parameter is in units of the 80 MHz core clock. (12.5 ns)
void SysTick_Wait(unsigned long delay){
NVIC_ST_RELOAD_R = delay-1; // number of counts to wait
NVIC_ST_CURRENT_R = 0; // any value written to CURRENT clears
while((NVIC_ST_CTRL_R&0x00010000) == 0){}
}
// 800000*12.5ns equals 10ms
void SysTick_Wait10ms(unsigned long delay){
unsigned long i;
for (i = 0; i < delay; i++){
SysTick_Wait(800000); // wait 10ms
}
}
int main(){
SysTick_Init();
PLL_Init();
SYSCTL_RCGC2_R = 0x02; // Using PORT B
GPIO_PORTB_DIR_R |= 0xFF; // ACTIVATE ALL PINS PB0 - PB7
GPIO_PORTB_DEN_R |= 0xFF; // DIGITAL ENABLE
GPIO_PORTB_AMSEL_R &=~ 0xFF; // DEACTIVATE ANALOG FEATURES
GPIO_PORTB_AFSEL_R &=~ 0xFF; // DEACTIVATE ALTERNATIVE FUNCTION
while(1){
//GPIO_PORTB_DATA_R &=~ 0XFE; // LED all clears
GPIO_PORTB_DATA_R = 0XEE; // Number 0 (7-segment display)
SysTick_Wait10ms(100); // added for delay for each segment
//GPIO_PORTB_DATA_R &=~ 0XFE;
GPIO_PORTB_DATA_R = 0X82; // Number 1
SysTick_Wait10ms(100);
//GPIO_PORTB_DATA_R &=~ 0XFE;
GPIO_PORTB_DATA_R = 0XDC; // Number 2
SysTick_Wait10ms(100);
//GPIO_PORTB_DATA_R &=~ 0XFE;
GPIO_PORTB_DATA_R = 0XD6; // Number 3
SysTick_Wait10ms(100);
//GPIO_PORTB_DATA_R &=~ 0XFE;
GPIO_PORTB_DATA_R |= 0XB2; // Number 4
SysTick_Wait10ms(100);
//GPIO_PORTB_DATA_R &=~ 0XFE;
GPIO_PORTB_DATA_R = 0X76; // Number 5
SysTick_Wait10ms(100);
//GPIO_PORTB_DATA_R &=~ 0XFE;
GPIO_PORTB_DATA_R = 0X7E; // Number 6
SysTick_Wait10ms(100);
//GPIO_PORTB_DATA_R &=~ 0XFE;
GPIO_PORTB_DATA_R = 0XC2; // Number 7
SysTick_Wait10ms(100);
//GPIO_PORTB_DATA_R &=~ 0XFE;
GPIO_PORTB_DATA_R = 0XFE; // Number 8
SysTick_Wait10ms(100);
//GPIO_PORTB_DATA_R &=~ 0XFE;
GPIO_PORTB_DATA_R = 0XF2; // Number 9
SysTick_Wait10ms(100);
}
return 0;
}
Subscribe to:
Posts (Atom)