Developing Programming to Suit The Hardware Configuration
Figure above show the operation of the
whole system. The program for the microcontroller must be configure and
referred to this block diagram. When the switch up is press the microcontroller
will allow the windows go up only when the current is below 7 ampere. Above 7
ampere current the microcontroller will give signal to the motor driver so that
the windows sliding down a little bit.
Developing program
for the MCU using Keil uVision5
Figure shows the program is
constructed using C language and the program is attached at below. the
program is need to be read the current sensor output from analog digital
converter ADC and if the current of the sensor is read 7A and above the motor
drive is need to change direction of the motor so that the power windows is not
continue going up.
//main POGRAM
#include <reg52.h>
#include "stdio.h"
#include "delays.c"
#include
"usart_header.c"
#include
"lcd4bitdriver.c"
#include
"adc0804driver.c"
sbit LED0 = P0^0;
//const unsigned char
d[]=(" Voltage ");
const unsigned char
d[]=("Voltage");
char buf[20];
signed int val;
void main(void)
{
char i,j,k;
unsigned int Vdig,Vdig_smlm,
loop ;
float Vana;
init_ADC0804();
init_usartBR9600();
Initialize_LCD();
printf("\n\r OK
PASS");
lcd_gotoxy(1,0);
puts_lcd("current");
delay_1ms(100);
while(1){
Vdig = ADC_read();
Vana = (float)Vdig*0.019608;
printf("\n\r%u :
%1.2f",Vdig, Vana);
if(Vdig!=Vdig_smlm){
sprintf(buf,"%u :
%1.2f",Vdig,Vana);
lcd_gotoxy(2,1);
puts_lcd(" ");
lcd_gotoxy(2,1);
puts_lcd(buf);
Vdig_smlm=Vdig;
}
if(Vana/0.040>7)
LED0=0;
if(Vana/0.040<6.5)
LED0=1;
}
}
*--------------------------------------------------------------------------
REG52.H
Header file for generic 80C52
and 80C32 microcontroller.
Copyright (c) 1988-2002 Keil
Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __REG52_H__
#define __REG52_H__
/* BYTE Registers */
sfr P0 = 0x80;
sfr P1 = 0x90;
sfr P2 = 0xA0;
sfr P3 = 0xB0;
sfr PSW = 0xD0;
sfr ACC = 0xE0;
sfr B = 0xF0;
sfr SP = 0x81;
sfr DPL = 0x82;
sfr DPH = 0x83;
sfr PCON = 0x87;
sfr TCON = 0x88;
sfr TMOD = 0x89;
sfr TL0 = 0x8A;
sfr TL1 = 0x8B;
sfr TH0 = 0x8C;
sfr TH1 = 0x8D;
sfr IE = 0xA8;
sfr IP = 0xB8;
sfr SCON = 0x98;
sfr SBUF = 0x99;
/* 8052 Extensions */
sfr T2CON = 0xC8;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2 = 0xCC;
sfr TH2 = 0xCD;
/* BIT Registers
*/
/* PSW */
sbit CY = PSW^7;
sbit AC = PSW^6;
sbit F0 = PSW^5;
sbit RS1 = PSW^4;
sbit RS0 = PSW^3;
sbit OV = PSW^2;
sbit P = PSW^0; //8052 only
/* TCON
*/
sbit TF1 = TCON^7;
sbit TR1 = TCON^6;
sbit TF0 = TCON^5;
sbit TR0 = TCON^4;
sbit IE1 = TCON^3;
sbit IT1 = TCON^2;
sbit IE0 = TCON^1;
sbit IT0 = TCON^0;
/* IE */
sbit EA = IE^7;
sbit ET2 = IE^5; //8052 only
sbit ES = IE^4;
sbit ET1 = IE^3;
sbit EX1 = IE^2;
sbit ET0 = IE^1;
sbit EX0 = IE^0;
/* IP */
sbit PT2 = IP^5;
sbit PS = IP^4;
sbit PT1 = IP^3;
sbit PX1 = IP^2;
sbit PT0 = IP^1;
sbit PX0 = IP^0;
/* P3 */
sbit RD = P3^7;
sbit WR = P3^6;
sbit T1 = P3^5;
sbit T0 = P3^4;
sbit INT1 = P3^3;
sbit INT0 = P3^2;
sbit TXD = P3^1;
sbit RXD = P3^0;
/* SCON
*/
sbit SM0 = SCON^7;
sbit SM1 = SCON^6;
sbit SM2 = SCON^5;
sbit REN = SCON^4;
sbit TB8 = SCON^3;
sbit RB8 = SCON^2;
sbit TI = SCON^1;
sbit RI = SCON^0;
/* P1 */
sbit T2EX = P1^1; // 8052 only
sbit T2 = P1^0; // 8052 only
/* T2CON
*/
sbit TF2 = T2CON^7;
sbit EXF2 = T2CON^6;
sbit RCLK = T2CON^5;
sbit TCLK = T2CON^4;
sbit EXEN2 = T2CON^3;
sbit TR2 = T2CON^2;
sbit C_T2 = T2CON^1;
sbit CP_RL2 = T2CON^0;
#endif
/*--------------------------------------------------------------------------
STDIO.H
Prototypes for standard I/O
functions.
Copyright (c) 1988-2002 Keil
Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __STDIO_H__
#define __STDIO_H__
#ifndef EOF
#define EOF -1
#endif
#ifndef NULL
#define NULL ((void *) 0)
#endif
#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif
#pragma SAVE
#pragma REGPARMS
extern char _getkey (void);
extern char getchar (void);
extern char ungetchar (char);
extern char putchar (char);
extern int printf (const char *, ...);
extern int sprintf (char *, const char *, ...);
extern int vprintf (const char *, char *);
extern int vsprintf (char *,
const char *, char *);
extern char *gets (char *,
int n);
extern int scanf (const char
*, ...);
extern int sscanf (char *,
const char *, ...);
extern int puts (const char
*);
#pragma RESTORE
#endif
/*
Please put
#include
"usart_header.c"
on top of main file.
Call init_usart(); once in
your code
Please take notes
USART in MODE1 Fosc = 11.0592
MHz
MODE = 1;
TIMER1 in MODE 2 (Auto
reload)
*/
#include <stdio.h>
/*
Call this function once in
the main file to work at 2400 b/s
*/
void init_usartBR2400(void){
PCON = 0x00;
SCON = 0x50; /* SCON: mode 1, 8-bit UART,
enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2,
8-bit reload */
TH1 = 244; /* TH1=244 reload value for 2400 baud */
TR1 = 1; /* TR1:
timer 1 run
*/
TI = 1;
}
/*
Call this function once in
the main file to work at 4800 b/s
*/
void init_usartBR4800(void){
PCON = 0x00;
SCON = 0x50; /* SCON: mode 1, 8-bit UART,
enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2,
8-bit reload */
TH1 = 250; /* TH1=250 reload value for 4800 baud */
TR1 = 1; /* TR1: timer 1 run */
TI = 1;
}
/*
Call this function once in
the main file to work at 9600 b/s
*/
void init_usartBR9600(void){
PCON = 0x00;
SCON = 0x50; /* SCON: mode 1, 8-bit UART,
enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2,
8-bit reload */
TH1 = 253; /* TH1=253 reload value for 9600 baud */
TR1 = 1; /* TR1: timer 1 run */
TI = 1;
}
Filename : lcd4bitdriver.c
Auhor : ISA
Date : 28/11/2013
Dependency : delays.c
Please put
#include
"lcd4bitdriver.c"
on top of main file.
2 x 16 LCD Driver - 4 bit interfacing as details
below.
The connection of control
lines and data should be as below
EN - P1.0
RS - P1.1
RW - P1.2
D4/D7 - P1.4/P1.7
This free codes are provided
as is without any guarantees or warranties.
Although the author has
attempted to find and correct any bugs in the codes, the author is not
responsible for any damage or
losses of any kind caused by the use or misuse of the codes.
The author is under no
obligation to provide support, service, corrections,
or upgrades to the programs.
Please email to
i.adam.mlk@gmail for any comments and visit my blogspots
(http://maelnotesonmicrocontrollerinterfacing.blogspot.com)
for any updates
*/
#include <reg52.h>
//LCD lines
#define D_LCD P2
sbit EN = D_LCD^0;
sbit RW = D_LCD^1;
sbit RS = D_LCD^2;
void Delay_uS(unsigned int
x);
unsigned char ADC_read(void);
const unsigned char
set_lcd_4bit[7]={0x20, 0x20, 0x80, 0x00, 0xe0, 0x00, 0x60};
void EN2HIGH(void);
void EN2LOW(void);
unsigned char
check_busy(void);
char read_data(void);
void send_control(char x,
char i);
void send_data(char x);
void Initialize_LCD(void);
void puts_lcd(char *pointer);
void lcd_gotoxy(char
line_number,char position);
/*
This function allows the
string to be displayed on LCD
example 1:
puts_lcd("saya");
puts_lcd(arr1);
puts_lcd(arr2);
*/
void puts_lcd(char *pointer){
while(*pointer) //
print till null encountered
send_data(*pointer++);
}
/*
This function directs the
cursor to Y and X of LCD.
Note:
Line_number is 1/2 correspond to upper or
lower ROW
Position is 0-15 correspond to column 0-15
example:
lcd_gotoxy(2,1);
result:
cursor will be placed
at ROW 2, COLUMN 1
*/
void lcd_gotoxy(char
line_number,char position){
char a;
if(line_number==1)
a=0x80+position;
else if(line_number==2)
a=0xC0+position;
send_control(a,2);
}
/*
This function returns status of
busy flag.
returns:
0
Not busy
1
Busy
example:
while(check_busy());
result:
the statement true if
LCD is in its busy state
*/
unsigned char
check_busy(void){
RS
= 0; // Logic ‘0’
RW = 1; //
Logic ‘1’
D_LCD|=0xf0; //
Set data line as input
delay_0dot1ms(1);
EN2HIGH();
if(D_LCD&0x80){
EN2LOW();
EN2HIGH();
EN2LOW();
D_LCD&=0x0f; // Set data line as output
return 1;
}
else{
EN2LOW();
EN2HIGH();
EN2LOW();
D_LCD&=0x0f; // Set data line as output
return 0;
}
}
/*
This function returns data
from CGRAM/DDRAM to controller
example:
x = read_data();
result:
x contains CGRAM/DDRAM data
*/
char read_data(void){
char x;
while(check_busy());
D_LCD|=0xf0; // Set
data line as input
RS = 1; //
Logic ‘1’
RW = 1; //
Logic ‘1’
delay_0dot1ms(1);
EN2HIGH();
x = D_LCD;
EN2LOW();
EN2HIGH();
x |= D_LCD>>4;
EN2LOW();
D_LCD&=0x0f;
// Set data line as output
return x;
}
/*
This function sends control
to LCD
example 1:
send_control(x,2);
result:
both nibble of
x will be sent to LCD
example 2:
send_control(x,1);
result:
only lower
nibble of x will be sent to LCD
*/
void send_control(char x,
char i){
while(check_busy());
D_LCD&=0x0f;
RS = 0;
RW = 0;
switch(i){
case 2:
D_LCD&=0x0f; // clear previous data
D_LCD |= x&0xf0;
delay_0dot1ms(1);
EN2HIGH();
EN2LOW();
case 1:
D_LCD&=0x0f;
D_LCD |= x<<4;
delay_0dot1ms(1);
EN2HIGH();
EN2LOW();
break;
default:
break;
}
}
/*
This function sends data to
LCD
example:
send_data(x);
result:
x will be sent to LCD
*/
void send_data(char x){
while(check_busy());
D_LCD&=0x0f;
// Set data line as output
RS
= 1; // Logic ‘0’
RW = 0; //
Logic ‘0’
D_LCD&=0x0f; // clear previous data
D_LCD |= x&0xf0;
delay_0dot1ms(1);
EN2HIGH();
EN2LOW();
D_LCD&=0x0f; // clear previous data
D_LCD |= x<<4;
delay_0dot1ms(1);
EN2HIGH();
EN2LOW();
}
/*
This function initialize the
LCD in 4-bit
void Initialize_LCD(void){
unsigned char i;
D_LCD = 0x00;
delay_0dot1ms(180); // more than 15 ms @ 12MHz
for(i=0; i<3; i++){
D_LCD &= 0x0f; //
clear previous data
D_LCD |= 0x30;
delay_0dot1ms(1);
EN2HIGH();
EN2LOW();
delay_1ms(5);
}
send_control(0x02,1);
send_control(0x28,2); // function
set
send_control(0x0e,2); // display
ON/OFF
send_control(0x06,2); // Entry
mode set
}
*/
/*
This function initialize the
LCD in 4-bit
must be called once in the
main program
*/
void Initialize_LCD(void){
char i;
D_LCD = 0x00; //
Set data line as output
delay_0dot1ms(152);
//
20ms @ 16MHz
for(i=0; i<7; i++){
D_LCD&=0x0f; //
clear previous data
D_LCD |= set_lcd_4bit[i];
delay_0dot1ms(1);
EN2HIGH();
EN2LOW();
}
}
void EN2HIGH(void){
EN=1;
delay_0dot1ms(1);
}
void EN2LOW(void){
EN=0;
delay_0dot1ms(1);
}
/*
Filename : adc0804driver.c
Auhor : ISA
Date : 28/11/2013
ADC0804 Driver as details
below.
The connection of control
lines and data should be as below
EOC - P3.4
CS - P3.5
WR - P3.6
RD - P3.7
D0/D7 - P1.0/P1.7
Please put
#include
"adc0804driver.c"
on top of main file.
Call init_ADC0804(); once in
your code
This free codes are provided
as is without any guarantees or warranties.
Although the author has
attempted to find and correct any bugs in the codes, the author is not
responsible for any damage or
losses of any kind caused by the use or misuse of the codes.
The author is under no
obligation to provide support, service, corrections,
or upgrades to the programs.
Please email to
i.adam.mlk@gmail for any comments and visit my blogspots
(http://maelnotesonmicrocontrollerinterfacing.blogspot.com)
for any updates
*/
#define D_ADC P1
#define C_ADC P3
sbit EOC = C_ADC^4;
sbit CS = C_ADC^5;
sbit WRS = C_ADC^6;
sbit RDS = C_ADC^7;
void init_ADC0804(void);
unsigned char ADC_read(void);
void init_ADC0804(void){
D_ADC = 0xFF;
// Set the port to 'read mode'
EOC=1;
}
/*
unsigned char ADC_read(void)
Returns 8-bit data from ADC
example:
i=ADC_read();
*/
unsigned char ADC_read(void){
unsigned char x;
CS=0;delay_10us(1); //
select ADC
WRS=0;
WRS=1; delay_10us(1);
CS=1;
while(EOC); // wait till end of conversion
CS=0;delay_10us(1);
RDS=0;delay_10us(1);
x=D_ADC; //
read data
RDS=1;
CS=1;
return x;
}
No comments:
Post a Comment