// east facing red light connected to PB5 // east facing yellow light connected
ID: 3349235 • Letter: #
Question
// east facing red light connected to PB5
// east facing yellow light connected to PB4
// east facing green light connected to PB3
// north facing red light connected to PB2
// north facing yellow light connected to PB1
// north facing green light connected to PB0
// north facing car detector connected to PE1 (1=car present)
// east facing car detector connected to PE0 (1=car present)
#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,3000,{goN,waitN,goN,waitN}},
{0x22, 500,{goE,goE,goE,goE}},
{0x0C,3000,{goE,goE,waitE,waitE}},
{0x14, 500,{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_PCTL_R &= ~0x000000FF; // 4) enable regular GPIO
GPIO_PORTE_DIR_R &= ~0x03; // 5) inputs on PE1-0
GPIO_PORTE_AFSEL_R &= ~0x03; // 6) regular function on PE1-0
GPIO_PORTE_DEN_R |= 0x03; // 7) enable digital on PE1-0
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];
}
}
how to do this traffic light code? thank you
as an FSM 2) The system starts with the LED on I 3) Wait about 62 ms 4) If the switch is pressed,then toggle the LED once, else turn the LED on. 5) Steps 3 and 4 are repeated over and over +3. 3V 3 V3 C1 u F U1e Booster Pack QIP SW3 NO switch R19 22% 1 2 74% PES R 1 10 k D1 LEDT1. 75 Re d GND PE1 27 PEO 18 7 40Explanation / Answer
As we have to wait for 62ms in the code .So, to make it possible we have to change the delay function from 10ms to 62 ms.
I am also writing 1 code which i used in a project of mine where i used the delay fuction of 10 ms by using microcontroller. I use the LED and control them by triggering the pulse. So, plese check that code also. It must help you.
Code=
#include<reg52.h>
static int y;
sbit x=P3^3;
void msecdelay2();
void msecdelay5();
void main()
{
P1=0x0ff; // MAKE PORT1 AS INPUT & OUTPUT PORT
P2=0x00; // MAKE PORT2 AS OUTPUT PORT
x=1;
while(1)
{
if(P1==0x0fe) //if switch 1 is pressed
{
y=0;
while(P1=0x0f5)
{
if(y==0)
{
zcr(); //WAIT FOR ZERO CROSS REFERENCE
msecdelay2(); // DELAY FOR 2MSEC
P2=0x0f;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y++; //increment y value
}
if(y==1)
{
zcr(); // WAIT FOR ZERO CROSS REFERENCE
msecdelay2(); // DELAY FOR 2MSEC
P2=0x0f;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y++; //increment y value
}
if(y==2)
{
P2=0xf0;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y++; //increment y value
}
if(y==3)
{
zcr(); // WAIT FOR ZERO CROSS REFERENCE
msecdelaY2(); // DELAY FOR 2MSEC
P2=0x0f0;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y=0;
}
}
}
else if(P1=0xfc) //if switch2 is pressed
{
y=0;
while(P1==0xfa)
{
if(y==0)
{
zcr(); //WAIT FOR ZERO CROSS REFERENCE
msecdelay2(); // DELAY FOR 2MSEC
P2=0x0f;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y++; //increment y value
}
if(y==1)
{
msecdelay2(); // DELAY FOR 2MSEC
P2=0x0f;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y++; //increment y value
}
if(y==2)
{
zcr(); // WAIT FOR ZERO CROSS REFERENCE
msecdelay2(); // DELAY FOR 2MSEC
P2=0x0f;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y++; //increment y value
}
if(y==3)
{
zcr(); // WAIT FOR ZERO CROSS REFERENCE
msecdelay2(); // DELAY FOR 2MSEC
P2=0xf0;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y++ //increment y value
}
if(y==4)
{
zcr(); // WAIT FOR ZERO CROSS REFERENCE
msecdelay2(); // DELAY FOR 2MSEC
P2=0xf0;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y++;
}
if(y==5)
{
zcr(); // WAIT FOR ZERO CROSS REFERENCE
P2=0xf0;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y=0;
}
}
}
else
{
if(y==0)
{
zcr(); // WAIT FOR ZERO CROSS REFERENCE
msecdelay2(); DELAY FOR 2MSEC
P2=0x0f;
msecdelay5(); // DELAY FOR 5MSEC
P2=0x00;
y++; //increment y value
}
if(y==1)
{
zcr(); // WAIT FOR ZERO CROSS REFERENCE
msecdelaY2(); // DELAY FOR 2MSEC
P2=0xf0;
msecdelaY5(); // DELAY FOR 5MSEC
P2=0x00;
y=0;
}
}
}
}
void zcr(void) // ZERO CROSS REFERENCE FUNCTION
{
while(x==1);
while(x==0);
}
void msecdelay2()
{
unsigned int i,j;
for(i=0;i<1;i++)
{
for(j=0;j<50;j++)
{;}
}
}
void msecdelay5()
{
unsigned int m,n;
for(m=0;m<5;m++)
{
for(n=0;n<100;n++);
}
}
Given code by you=
/ east facing red light connected to PB5
// east facing yellow light connected to PB4
// east facing green light connected to PB3
// north facing red light connected to PB2
// north facing yellow light connected to PB1
// north facing green light connected to PB0
// north facing car detector connected to PE1 (1=car present)
// east facing car detector connected to PE0 (1=car present)
#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,3000,{goN,waitN,goN,waitN}},
{0x22, 500,{goE,goE,goE,goE}},
{0x0C,3000,{goE,goE,waitE,waitE}},
{0x14, 500,{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_PCTL_R &= ~0x000000FF; // 4) enable regular GPIO
GPIO_PORTE_DIR_R &= ~0x03; // 5) inputs on PE1-0
GPIO_PORTE_AFSEL_R &= ~0x03; // 6) regular function on PE1-0
GPIO_PORTE_DEN_R |= 0x03; // 7) enable digital on PE1-0
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_Wait62ms(FSM[S].Time);
Input = SENSOR; // read sensors
S = FSM[S].Next[Input];
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.