Page designed to display information pertaining to ECE 4951 [System Design With MicroControllers]
Created by Steven Brehmer [11/18/2015]
// *********************************************************************
// ** Class: ECE 4951 **
// ** Project: Project 1A {F1, F2, A1} **
// ** PIC 1 CODE **
// ** Group: Steven Brehmer, Tarik Syed, Ali **
// ** Date: 11/11/2015 **
// *********************************************************************
// Basic Schematic
// -------------------
// *Key - P3 = Pickit 3
// - PC1 = PIC16f690
// - PC2 = PIC16f690
// - LCD = LCD
// - MC = Motor Control = SN754410
// - RW+ = Right Wheel Positive
// - RW- = Right Wheel Negative
// - LW+ = Left Wheel Positive
// - LW- = Right Wheel Negative
// - LS = Left Sensor
// - RS = Right Sensor
// - CS = Center Sensor
// - GND = Ground
//
//
// ________1_______
// 5v----|1 U 20|------P3 Pin2
// -|2 19|------P3 Pin3
// -|3 18|------P3 Pin4
// P3 Pin1------|4 17|-
// MC Pin 7-----|5 16|------MC Pin10
// MC Pin 2-----|6 15|------MC Pin15 / -----5V
// CS-----------|7 14|------50Cm Flag from PC2
// RS-----------|8 13|------Status Bit0
// LS-----------|9 12|------Status Bit1
// -|10 11|------Status Bit2
// ____PIC16F690___
//
//
// ________________
// 5v--10K Ohm--|1 U 16|------5v
// PC1 Pin6-----|2 15|------PC1 Pin 15
// LW+ ---------|3 14|------RW+
// GND---|4 13|---GND
// GND---|5 12|---GNd
// LW- ---------|6 11|------RW-
// PC1 Pin5-----|7 10|------PC1 Pin16
// 5v---|8 9|---5v
// ____SN754410____
//
//
//
// _______2________
// 5v----|1 U 20|------P3 Pin2
// LCD6---------|2 19|------P3 Pin3
// LCD5---------|3 18|------P3 Pin4
// P3 Pin1------|4 17|------LCD4
// LCD12--------|5 16|------LCD7
// LCD11--------|6 15|------LCD8
// LCD10--------|7 14|------LD9
// LCD13--------|8 13|------RWheel Counter
// LCD14--------|9 12|------RWheel Counter
// -|10 11|------50Cm Flag in PC1
// ____PIC16F690___
#define RRVS PORTC.B4 // Left Wheel Reverse - Pin 6
#define RFWD PORTC.B5 // Left Wheel Forward - Pin 5
#define LFWD PORTC.B0 // Right Wheel Forward - Pin 16
#define LRVS PORTC.B1 // Right Wheel Reverse - Pin 15
#define SC PORTC.B3 // Center Sensor - Pin 7
#define SR PORTC.B6 // Right Sensor - Pin 8
#define SL PORTC.B7 // Left Sensor - Pin 9
#define CMFlag PORTC.B2 // SWITCH - Pin 14
#define DELAY 50
//Status Pin Defines
#define SB0 PORTB.B4
#define SB1 PORTB.B5
#define SB2 PORTB.B6
//LineFollow Functions
//--------------------
// Forward() - Both Right and Left Wheels Forward
// TurnRight() - Only LeftWheel Forward
// TurnLeft() - Only Right Wheel Forward
// TurnAround() - Left Forward Right Reverse
//Line Avoid Functions
//--------------------
// BigTurnRight() - Left Wheel Forward with Delay so it moves around Center of Circle
// BigTurnLeft() - Right Wheel Forward with Delay so it moves around Center of Circle
// Forward() - Both Wheel Forward
// TurnAround() - Same as Line Follow
void Forward()
{
RFWD =0;
LFWD =0;
RRVS =1;
LRVS =1;
}
void TurnRight()
{
RFWD =1;
LFWD =0;
RRVS =1;
LRVS =1;
}
void BigTurnRight()
{
RFWD =1;
LFWD =0;
RRVS =0;
LRVS =1;
delay_ms(1000);
}
void TurnLeft()
{
RFWD =0;
LFWD =1;
RRVS =1;
LRVS =1;
}
void BigTurnLeft()
{
RFWD =0;
LFWD =1;
RRVS =1;
LRVS =0;
delay_ms(1000);
}
void TurnAround()
{
RFWD =0;
LFWD =1;
RRVS =1;
LRVS =0;
delay_ms(50);
}
void AllOff()
{
RFWD =1;
LFWD =1;
RRVS =1;
LRVS =1;
}
void LineFollow()
{
if(SC ==0 ) //On Center Sensor -> Go Forward
{
Forward();
}
else if(SR ==0) //On right Sensor - > turn left
{
TurnLeft();
}
else if(SL ==0) //On left Sensor - > turn right
{
TurnRight();
}
else if( SL !=0 && SR !=0 && SC !=0) // If All Read Black, Then Turn around to find the line
{
TurnAround();
}
else
{
}
}
void LineFollowCM()
{
if(SC ==0 ) //On Center Sensor -> Go Forward
{
Forward();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else if(SR ==0) //On right Sensor - > turn left
{
TurnLeft();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else if(SL ==0) //On left Sensor - > turn right
{
TurnRight();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else if( SL !=0 && SR !=0 && SC !=0) // If All Read Black, Then Turn around to find the line
{
TurnAround();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else
{
}
}
void Avoid()
{
if(SC ==0 ) //On Center Sensor -> Go Forward
{
Forward();
}
else if(SR ==0) //On right Sensor - > turn left
{
BigTurnLeft();
}
else if(SL ==0) //On left Sensor - > turn right
{
BigTurnRight();
}
else if( SL !=0 && SR !=0 && SC !=0) // If All Read Black, Then Turn around to find the line
{
Forward();
}
else
{
}
}
void AvoidCM()
{
if(SC ==0 ) //On Center Sensor -> Go Forward
{
Forward();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else if(SR ==0) //On right Sensor - > turn left
{
BigTurnLeft();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else if(SL ==0) //On left Sensor - > turn right
{
BigTurnRight();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else if( SL !=0 && SR !=0 && SC !=0) // If All Read Black, Then Turn around to find the line
{
Forward();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else
{
}
}
void main()
{
int ON_OFF = 1;
int squarePrev =0;
int square_count =0;
int TriPrev = 0;
int Tri_count =0;
ANSEL = 0;
ANSELH = 0;
TRISA = 0xff;
TRISB = 0xff;
TRISC = 0XFF;
PORTC = 0;
//Define Port C Outputs
TRISC.B0 = 0;
TRISC.B1 = 0;
TRISC.B4 = 0;
TRISC.B5 = 0;
TRISC.B2 =1; // 50 Cm Flag
TRISB.B6 = 1;
//RCL Inputs
TRISC.B3=1;
TRISC.B6=1;
TRISC.B7=1;
//Program Bit inputs
TRISB.B4 = 1; //B0
TRISB.B5 = 1; //B1
TRISB.B6 = 1; //B2
//Initalize as all OFF
LFWD = 1;
LRVS = 1;
RFWD = 1;
RRVS = 1;
while(ON_OFF == 1)
{
if((SB0 == 0) & (SB1 == 0) & (SB2 == 0)) //OFF
{
AllOff();
}
else if((SB0 == 0) & (SB1 == 0) & (SB2 == 1))// ACTIVE
{
AllOff();
}
else if((SB0 == 0) & (SB1 == 1) & (SB2 == 0))// Line Follow
{
LineFollow();
}
else if((SB0 == 0) & (SB1 == 1) & (SB2 == 1)) //Line Avoid
{
Avoid();
}
else if((SB0 == 1) & (SB1 == 0) & (SB2 == 0)) //50 cm Line Follow
{
if(CMFlag == 0)
{
LineFollowCM();
}
else
{
ON_OFF=0;
}
}
else if((SB0 == 1) & (SB1 == 0) & (SB2 == 1)) //50 Cm Line Avoid
{
if(CMFlag == 0)
{
AvoidCM();
}
else
{
ON_OFF = 0;
}
}
else if((SB0 == 1) & (SB1 == 1) & (SB2 == 0)) //Square
{
if(CMFlag == 0 & square_count <=4)
{
squarePrev = 1;
Forward();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else
{
TurnLeft();
delay_ms(1200);
square_count++;
}
if(square_count == 4)
{
AllOff();
ON_OFF =0;
}
}
else if((SB0 == 1) & (SB1 == 1) & (SB2 == 1)) //Triangle
{
if(CMFlag == 0 & Tri_count <=3)
{
TriPrev = 1;
Forward();
delay_ms(DELAY);
AllOff();
delay_ms(250);
}
else
{
TurnLeft();
delay_ms(1500);
Tri_count++;
}
if(Tri_count == 3)
{
AllOff();
ON_OFF =0;
}
}
else
{
AllOff();
}
}
}
// *********************************************************************
// ** Class: ECE 4951 **
// ** Project: Project 1A {F1, F2, A1} **
// ** PIC 2 CODE **
// ** Group: Steven Brehmer, Tarik Syed, Ali **
// ** Date: 11/11/2015 **
// *********************************************************************
// Basic Schematic
// -------------------
// *Key - P3 = Pickit 3
// - PC1 = PIC16f690
// - PC2 = PIC16f690
// - LCD = LCD
// - MC = Motor Control = SN754410
// - RW+ = Right Wheel Positive
// - RW- = Right Wheel Negative
// - LW+ = Left Wheel Positive
// - LW- = Right Wheel Negative
// - LS = Left Sensor
// - RS = Right Sensor
// - CS = Center Sensor
// - GND = Ground
//
//
// ________1_______
// 5v----|1 U 20|------P3 Pin2
// -|2 19|------P3 Pin3
// -|3 18|------P3 Pin4
// P3 Pin1------|4 17|-
// MC Pin 7-----|5 16|------MC Pin10
// MC Pin 2-----|6 15|------MC Pin15 / -----5V
// CS-----------|7 14|------50Cm Flag from PC2
// RS-----------|8 13|------Status Bit0
// LS-----------|9 12|------Status Bit1
// -|10 11|------Status Bit2
// ____PIC16F690___
//
//
// ________________
// 5v--10K Ohm--|1 U 16|------5v
// PC1 Pin6-----|2 15|------PC1 Pin 15
// LW+ ---------|3 14|------RW+
// GND---|4 13|---GND
// GND---|5 12|---GNd
// LW- ---------|6 11|------RW-
// PC1 Pin5-----|7 10|------PC1 Pin16
// 5v---|8 9|---5v
// ____SN754410____
//
//
//
// _______2________
// 5v----|1 U 20|------P3 Pin2
// LCD6---------|2 19|------P3 Pin3
// LCD5---------|3 18|------P3 Pin4
// P3 Pin1------|4 17|------LCD4
// LCD12--------|5 16|------LCD7
// LCD11--------|6 15|------LCD8
// LCD10--------|7 14|------LD9
// LCD13--------|8 13|------RWheel Counter
// LCD14--------|9 12|------RWheel Counter
// -|10 11|------50Cm Flag in PC1
// ____PIC16F690___
/*
* LCD pin out GND power Contrast RS =>RA2 R/W =>RA4 E => RA5
Data =>
* PORTC (8 bit interface)
*/
#define RS PORTA.B2
#define RW PORTA.B4
#define E PORTA.B5
#define TRUE 1
#define FALSE 0
/*
* ASSUME E is always zero except as needed
*/
/*
* sendany: The lowest level function to send a byte to lcd
Load PORTC
* with the byte you want to send raise E delay lower E
*/
void sendany(char c)
{
PORTC = c;
RW = 0;
E = 1;
delay_us(5);
E = 0;
}
/*
* send_command: Sends a command RS=0 use sendany to actually
send the
* command delay
*/
void send_command(char cmd)
{
RS = 0;
sendany(cmd);
delay_ms(37);
}
/*
* send_data: Sends a data to be displayed RS=1 use sendany to
actually
* send the value delay
*/
void send_data(char value)
{
RS = 1;
sendany(value);
delay_ms(37);
}
/*
* lcdinit: Send the commands as required by the datasheet
*/
void lcdinit()
{
delay_ms(40); // warmup
send_command(0x30);
delay_ms(100); // First command needs more
//delay
send_command(0x30);
send_command(0x30);
send_command(0x38);
send_command(0x10);
send_command(0x0c);
send_command(0x06);
}
/*
* Useful commands
clear: clear the LCD screen
move: Moves the cursor the the LCD
*/
void clear()
{
send_command(0x01);
}
void move(char row, char col)
{
char cmd;
// Set DDRM address
row = row & 1; // only 0 or 1 permitted
col = col & 0x3F; // only 0 to 3F
cmd = 0x80 + row * 0x40 + col;
send_command(cmd);
}
void init()
{
PORTC = 0;
E = 0;
RW = 0;
TRISA = TRISC = 0x00;
ANSEL = ANSELH = 0;
lcdinit();
}
/*
* send_string: Sends a string. This is a generic string
printing routine
*/
void send_string(char *s)
{
for (; *s != 0; s++)
send_data(*s);
}
void main()
{
int count = 1;
char col = 0;
int i = 1;
short r = 0;
int ri = 0;
short l=0;
int li = 0;
short avg = 0;
int avgi =0;
short max = 17;
short Rchange = FALSE;
short Rprevious = FALSE;
short RnewVal = FALSE;
short Lchange = FALSE;
short Lprevious = FALSE;
short LnewVal = FALSE;
char txt[4];
char c[4];
char p[4];
char n[4];
TRISB.B4 =1;
TRISB.B5 =1;
TRISB.B6 = 0;
PORTB.B6=0;
init();
clear();
while (1)
{
if(PORTB.B4 == 0)
{
RnewVal = TRUE; // on
}
else
{ //OFF
RnewVal = FALSE;
}
if( Rprevious == RnewVal)
{
Rchange = FALSE;
}
else
{
Rchange = TRUE;
}
if(Rchange == TRUE)
{
r++;
ri++;
ByteToStr(r, txt);
//clear();
move(1,0);
send_string("R");
move(1,1);
send_string(txt);
Rchange = FALSE;
}
else
{
}
Rprevious = RnewVal;
if(PORTB.B5 == 0)
{
LnewVal = TRUE;
}
else
{
LnewVal = FALSE;
}
if( Lprevious == lnewVal)
{
Lchange = FALSE;
}
else
{
Lchange = TRUE;
}
if(Lchange == TRUE)
{
l++;
li++;
ByteToStr(l, txt);
//clear();
move(1,5);
send_string("L");
move(1,6);
send_string(txt);
Lchange = FALSE;
}
else
{
}
Lprevious = LnewVal;
avg = (l+r)/2;
avgi = (li +ri)/2;
ByteToStr(avg, txt);
//clear();
move(1,10);
send_string("A");
move(1,11);
send_string(txt);
if((txt[0] == ' ') & (txt[1] == '2') & (txt[2] == '3'))
{
PORTB.B6 = 1;
delay_ms(300);
PORTB.B6 = 0;
r = 0;
ri = 0;
l=0;
li = 0;
avg = 0;
avgi =0;
}
PORTB.B6 = 0;
}
}