Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Programming

No description
by

badrul hisham

on 23 November 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Programming

Programming
Basic Programming and How to Program an Autocar
Autocar Programming
Basic Programming
INTRODUCTION
A line following robot.
The line is usually black and it is on a white surface.
The robot sense the line to move.
Input and output pins declaration
Input are the signal or data receives by the system
Example:Sensors and buttons
Output are the signal or data sent from it
LED,motors
Input:PORTAbits.RA0
Output:LATAbits.LATA0
Input: #define button1 PORTCbits.RC4
Output: #define led1 LATDbits.LATD0
#define " any name " name of PIC pin
Flowchart
Used in designing and documenting complex processes or programs.
Helps visualize what is going on and understand the process
Helps in finding flaws ,bottleneck and other less obvious features.
Objective:
1.Workflow Management and Continuous Improvement
-To analyze your processes for waste and inefficiency
-To identify areas for process improvement

2.Programming
-To flesh out the programming logic
-Represents an algorithm in graphical symbols

3.Troubleshooting guides
-To trace the algorithm
Function Prototype
Create function prototype for the function that is created below the main function
Example:
void forward (void);
void init(void);
void stop(void);
void linefollow1(void);

Variables Declaration:
Declare vaiables to be used in the program
Example:
char mode;
unsigned int speed;
float speed;
Main Function:
The code must have
only one
main function where the program will start to run
void main (void)
{ "call sub-function }
TRIS Register Setting
TRIS setting is a setting to tell the microprocessor that the pin is used as input or output
If the TRIS setting is set wrongly,the program will be unstable
Usually this setting is written inside the main function
Logic '1' is used to set as input while logic '0' is to set as an output.
Sub-function
Other functions besides main function
To make the code more organized
Simplifies the main function
Example:
PORT A Input(RA0 ,RA1)
PORT B Output(RB1,RB2)
PORT C Input(RC0,RC1,RC5)
Output(RC3,RC4)
TRISA0b00000011
TRISB0b00000000
TRISC0b00100111
int main (void)
{
while(1) {
if (sw==1)
{
led1=1
}
else
{
led1=0;
}

Selection Structure
Allows the program to decide an action based on the user's input or other processes
If-else construct:
If the condition (expression) is true,statement1 is executed and if it is a false,statement2 is executed.
If-else if construct:
if (condition_1) {
statement_1;
} else if (condition_2) {
statement_2;
............
} else if (condition_n) {
statement_n;
} else {
last_statement;
}
If condition_1 is true,statement_1 is executed and if it false ,condition_2 is checked.If condition_2 is also false,the condition is checked until it is true .If all the conditions are false,the last_statement will be executed.
Switch statement:
switch (variable) {
case expression_1:
statement_1;
break;
case expression_2:
statement_2;
break;
default:
default statement;
break;
}

switch (mode) {
case 1:
LED1=1;
break;
case 2:
LED2=1;
break;
default:
LED1=0;
LED2=0;
break;
}
If the variables matches the variable specified after one of the case keywords,the following staement will be executed.
Example:
if (expression)
statement1;
else
statement2;
Pin
Input
Output
3. (RA1)
17.(RC2)
26. (RC7)
9. (RE1)
PORTAbits.RA1

LATAbits.LATA1
PORTCbits.RC2
LATCbits.LATC2
PORTCbits.RC7
PORTEbits.RE1
LATCbits.LATC7
LATEbits.LATE1
Remember: Input=1
Output=0
Preprocessor Directory
To let the microcontrollers know the registers' name that is used in the code
Example: pic18F4520
#include <p18f452.h>
void main(void)
{
init();
while (1)
{
if (B1==0)
{while(1)
{ LED2=1;
linefollow();
}
}
else if (B2==0)
{while(1)
{ LED1=LED3=1;


}
}
else
{ stop();
void linefollow(void)
{
if((sensor1 == 1) && (sensor2 == 0) && (sensor3 == 0) && (sensor4 == 1)) //1001
{
forward (150,150);
}

if((sensor1 == 0) && (sensor2 == 1) && (sensor3 == 1) && (sensor4 == 0)) //0110
{
forward (200,200);
}
void stop(void)
{
motor_right1=0;
motor_right2=0;
motor_left1=0;
motor_left2=0;
pwm1=0;
pwm2=0;
}
Full transcript