Tuesday, 29 September 2015

TIMER2 MODULE OF PIC16F877A

Timer2 is an 8-bit timer with a prescaler and a postscaler. It can be used as the PWM time base for the PWM mode of the CCP module(s). The TMR2 register is readable and writable and is cleared on any device Reset. The input clock (FOSC/4) has a prescale option of 1:1, 1:4 or 1:16, selected by control bits T2CKPS1:T2CKPS0 (T2CON<1:0>).The Timer2 module has an 8-bit period register, PR2.Timer2 increments from 00h until it matches PR2 and then resets to 00h on the next increment cycle. PR2 is a readable and writable register. The PR2 register is initialized to FFh upon Reset.
The match output of TMR2 goes through a 4-bit postscaler (which gives a 1:1 to 1:16 scaling inclusive) to generate a TMR2 interrupt (latched in flag bit, TMR2IF (PIR1<1>)). Timer2 can be shut-off by clearing control bit, TMR2ON (T2CON<2>), to minimize power consumption.

Program to Generate a 1s delay ....

#include<pic.h>
void timer();
void main()
{
TRISD=0X00;
T2CON=0X7E;
while(1)
{
PORTD=0XFF;
timer();
PORTD=0X00;
timer();
}
}
void timer()
{
int i;
TMR2=0X00;
for(i=0;i<15;i++)
{
while(TMR2IF==0);
TMR2IF=0;
}
}

Generate a 1s delay using TIMER1 in PIC16F877A

#include<pic.h>
void timer();
void main()
{
TRISD=0X00;
T1CON=0X39;
while(1)
{
PORTD=0XFF;
timer();
PORTD=0X00;
timer();
}
}
void timer()
{
int i;
TMR1L=0X00;
TMR1H=0X00;
for(i=0;i<2;i++)
{
while(TMR1IF==0);
TMR1IF=0;
}
}

TIMER1 MODULE OF PIC16F877A

The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and TMR1L) which are readable and writable. The TMR1 register pair (TMR1H:TMR1L) increments from 0000h to FFFFh and rolls over to 0000h. The TMR1 interrupt, if enabled, is generated on overflow which is latched in interrupt flag bit, TMR1IF (PIR1<0>). This interrupt can be enabled/disabled by setting/clearing TMR1 interrupt enable bit, TMR1IE (PIE1<0>). Timer1 can operate in one of two modes:
• As a Timer
• As a Counter
The operating mode is determined by the clock select bit, TMR1CS (T1CON<1>).In Timer mode, Timer1 increments every instruction cycle. In Counter mode, it increments on every rising
edge of the external clock input. Timer1 can be enabled/disabled by setting/clearing
control bit, TMR1ON (T1CON<0>). Timer1 also has an internal “Reset input”. This Reset
can be generated by either of the two CCP modules.When the Timer1 oscillator is enabled (T1OSCEN is set), the RC1/T1OSI/CCP2 and RC0/T1OSO/T1CKI pins become inputs. That is, the TRISC<1:0> value is ignored and these pins read as ‘0’. Additional information on timer modules is available in the PICmicro® Mid-Range MCU Family Reference Manual (DS33023).

Digital Clock using Timer0 in PIC16F877A

#include<pic.h>
void print(int);
void delay();
void dela();
void cmd(int);
void data(char);
char keypad();
void main()
{
int i,j=0,k=0,l=0,o=0;
int a,b,c,d,e,f;
OPTION=0X07;
TRISB=0X00;
TRISC=0X00;
TRISD=0X0F;
cmd(0x38);
cmd(0X0E);
cmd(0X06);
cmd(0X01);
cmd(0X80);
delay();
a=keypad();
data(a);
delay();
b=keypad();
data(b);
delay();
data(':');
c=keypad();
data(c);
delay();
d=keypad();
data(d);
delay();
data(':');
e=keypad();

data(e);
delay();
f=keypad();
data(f);
j=(e-48)*10+(f-48);
k=(c-48)*10+(d-48);
l=(a-48)*10+(b-48);

while(1)
{
TMR0=0X00;
for(i=0;i<15;i++)
{
while(TMR0IF==0);
TMR0IF=0;
}
cmd(0X80);
j++;
if(j>59)
{
j=0;
k++;
}
if(k>59)
{
k=0;
l++;
}
if(l>11)
{
l=0;
cmd(0X01);
o++;
}
print(l);
data(':');
print(k);
data(':');
print(j);
if(o%2==0)
{
data(' ');
data(' ');
data('A');
data('M');
}
else
{
data(' ');
data(' ');
data('P');
data('M');
}
}
}
void print(int h)
{
int c,p,a[2],q;
if(h==0)
data(0+48);
for(p=0;h>0;p++)
{
c=h%10;
h=h/10;
a[p]=c;
}
for(q=p-1;q>=0;q--)
data(a[q]+48);
}
void cmd(int s)
{
PORTB=s;
RC0=0;
RC1=1;
dela();
RC1=0;
}

void data(char a)
{
PORTB=a;
RC0=1;
RC1=1;
dela();
RC1=0;
}
void dela()
{int i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++);
}
void delay()
{int i,j;
for(i=0;i<100;i++)
for(j=0;j<1000;j++);
}
char keypad()
{
int e;
PORTD=0X7F;
e=PORTD&0X0F;
switch(e)
{
case 0X07:
return '1';
break;
case 0X0B:
return '2';
break;
case 0X0D:
return '3';
break;
case 0X0E:
return('4');
break;
}
PORTD=0XBF;
e=PORTD&0X0F;
switch(e)
{
case 0X07:
return('4');
break;
case 0X0B:
return('5');
break;
case 0X0D:
return('6');
break;
case 0X0E:
return('7');
break;
}
PORTD=0XDF;
e=PORTD&0X0F;
switch(e)
{
case 0X07:
return('8');
break;
case 0X0B:
return('9');
break;
case 0X0D:
return('0');
break;
case 0X0E:
return('A');
break;
}
PORTD=0XEF;
e=PORTD&0X0F;
switch(e)
{
case 0X07:
return('B');
break;
case 0X0B:
return('C');
break;
case 0X0D:
return('D');
break;
case 0X0E:
return('E');
break;
}
}

Program to Blink PORTC when TIMER0 overflows....



#include<pic.h>
void delay(int);
void main()
{
TRISC=0X00;
OPTION=0X07;
TMR0=0X00; 
while(TMR0IF==0);
while(1)
{
PORTC=0XFF;
delay(10);
PORTC=0X00;
delay(10);
}
}
void delay(int k)
{
int j,i;
for(i=0;i<k;i++)
for(j=0;j<1000;j++);
}

TIMER0 MODULE OF PIC16F877A

The Timer0 module timer/counter has the following
features:
• 8-bit timer/counter
• Readable and writable
• 8-bit software programmable prescaler
• Internal or external clock select
• Interrupt on overflow from FFh to 00h
• Edge select for external clock





Figure  is a block diagram of the Timer0 module andthe prescaler shared with the WDT.
Additional information on the Timer0 module is available in the PICmicro® Mid-Range MCU Family
Reference Manual (DS33023). Timer mode is selected by clearing bit T0CS (OPTION_REG<5>). In Timer mode, the Timer0 module will increment every instruction cycle (without prescaler). If the TMR0 register is written, the increment is inhibited for the following two instruction cycles.
The user can work around this by writing an adjusted value to the TMR0 register.Counter mode is selected by setting bit T0CS (OPTION_REG<5>). In Counter mode, Timer0 will increment either on every rising or falling edge of pin RA4/T0CKI. The incrementing edge is determined by the Timer0 Source Edge Select bit, T0SE (OPTION_REG<4>). Clearing bit T0SE selects the rising edge.

UART TRANSMITTER IN ARM7(LPC2129/38)

#include<lpc21xx.h>
int main()
{

PINSEL0=0X00000001;
U0LCR=0X83;
U0DLL=0X51;
U0DLM=0X00;
U0LCR=0X03;
U0THR='A';
while((U0LSR&0X20)==0);
while(1);
}

UART RECEIVER IN ARM7(LPC2129/38)

#include<lpc21xx.h>
void cmd(int);
void cmd1(int);
void data(char);
void delay();
int main()
{
char s;
PINSEL0=0X00000004;
IO1DIR=0XFFFFFFFF;
cmd(0x30);
cmd(0x30);
cmd(0x30);
cmd(0x20);
cmd1(0x28);
cmd1(0x01);
cmd1(0x06);
cmd1(0x0e);
cmd1(0x80);

U0LCR=0X83;
U0DLL=0X51;
U0DLM=0X00;
U0LCR=0X03;

while(1)
{
while((U0LSR&0X01)==0);
s=U0RBR;
data(s);
 }
 }

 void cmd(int b)
{
unsigned int s;
s=b&0xf0;
IO1SET=s<<16;
IO1CLR=0X000F0000;
IO1SET|=0X00080000;
delay();
IO1CLR=0X00080000;
IO1CLR=0XFFFFFFFF;
}
void cmd1(int a)
{


unsigned int s;
s=a&0xf0;
IO1SET=s<<16;
IO1CLR=0X000F0000;
IO1SET|=0X00080000;
delay();
IO1CLR=0X00080000;
IO1CLR=0XFFFFFFFF;


s=a&0x0f;
IO1SET=s<<20;
IO1CLR=0X000F0000;
IO1SET|=0X00080000;
delay();
IO1CLR=0X00080000;
delay();
IO1CLR=0XFFFFFFFF;
}
void data(char a)
{

char s;
s=a&0xf0;
IO1SET=s<<16;
IO1CLR=0X000F0000;
IO1SET|=0X000A0000;
delay();
IO1CLR=0X00080000;
IO1CLR=0XFFFFFFFF;


s=a&0x0f;
IO1SET=s<<20;
IO1CLR=0X000F0000;
IO1SET|=0X000A0000;
delay();
IO1CLR=0X00080000;
delay();
IO1CLR=0XFFFFFFFF;
}
void delay()
{
int i;
for(i=0;i<10000;i++);
}

Saturday, 26 September 2015

UART : ARM7(LPC2129/38)


The LPC2129/38  each contain two UARTs. In addition to standard transmit and receive data lines, the UART1 also provides a full modem control handshake interface.

Features
 • 16 B Receive and Transmit FIFOs.
 • Register locations conform to 16C550 industry standard.
 • Receiver FIFO trigger points at 1 B, 4 B, 8 B, and 14 B.
 • Built-in fractional baud rate generator covering wide range of baud rates without a need for external      crystals of particular values.
 • Transmission FIFO control enables implementation of software (XON/XOFF) flow control on both    UARTs
 • UART1 is equipped with standard modem interface signals. This module also provides full support    for hardware flow control (auto-CTS/RTS).



Separate the data between # and * from serial receiver and display it on LCD(LM016L) in ATMEGA32L


#include <avr/io.h>
#include <util/delay.h>
void cmd(int);
void data(char);
int main(void)
{  
       char a;
int q,d;
        DDRB=0XFF;
DDRA=0XFF;
        DDRD=0xfe;
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x19;
cmd(0x38);
cmd(0x01);
cmd(0x0e);
cmd(0x06);
cmd(0x80);
    while(1)
    {
        while((UCSRA&0x80)==0);

a=UDR;
if(a=='*')
q=0;
if(q==1)
data(a);
if(a=='#')
q=1;

UCSRA=0x00;
    }
}
void cmd(int a)
{
PORTB=a;
PORTA=0x00;
PORTA=0x04;
_delay_ms(200);
PORTA=0x00;

}
void data(char s)
{
PORTB=s;
PORTA=0x01;
PORTA=0x05;
_delay_ms(200);
PORTA=0x01;
}

USART ASYNCHRONOUS RECEIVER (ATMEGA32L)



#include <avr/io.h>
#include <util/delay.h>
void cmd(int);
void data(char);
int main(void)
{   DDRB=0XFF;
DDRA=0XFF;
    char a;
    DDRC=0xfe;
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x19;
cmd(0x38);
cmd(0x01);
cmd(0x0e);
cmd(0x06);
cmd(0x80);
    while(1)
    {
        while((UCSRA&0x80)==0);


data(UDR);
_delay_ms(1000);
UCSRA=0x00;
    }
}
void cmd(int a)
{
PORTB=a;
PORTA=0x00;
PORTA=0x04;
_delay_ms(200);
PORTA=0x00;

}
void data(char s)
{
PORTB=s;
PORTA=0x01;
PORTA=0x05;
_delay_ms(200);
PORTA=0x01;
}

USART ASYNCHRONOUS TRANSMITTER (ATMEGA32)


#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
        int i;
char S[]="hello";
        DDRD=0x02;
        UCSRA=0X00;
UCSRB=0X18;
UCSRC=0X06;

UBRRL=0X19;
UBRRH=0X00;

     for (i=0;S[i]!='\0';i++)
    {
    
UDR=S[i];
while((UCSRA&0X40)==0);
UCSRA=0X00;
_delay_ms(1000);
    }
while(1);
}
        

Friday, 25 September 2015

ATMEGA32L:USART

The Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART) is a highly flexible serial communication device.
The main features are:
• Full Duplex Operation (Independent Serial Receive and Transmit Registers)
• Asynchronous or Synchronous Operation
• Master or Slave Clocked Synchronous Operation
• High Resolution Baud Rate Generator
• Supports Serial Frames with 5, 6, 7, 8, or 9 Data Bits and 1 or 2 Stop Bits
• Odd or Even Parity Generation and Parity Check Supported by Hardware
• Data OverRun Detection
• Framing Error Detection
• Noise Filtering Includes False Start Bit Detection and Digital Low Pass Filter
• Three Separate Interrupts on TX Complete, TX Data Register Empty, and RX Complete
• Multi-processor Communication Mode
• Double Speed Asynchronous Communication Mode


USART Registers

USART I/O Data Register – UDR

USART Control and Status Register A –UCSRA

USART Control and Status Register B –UCSRB

USART Control and Status Register C –UCSRC

USART Baud Rate Registers – UBRRL and UBRRH

USART Block Diagram





Display the characters between * and # using USART in PIC16F877A

#include<pic.h>
void main()
{

char c;
TRISC=0X80;
TXSTA=0X24;
RCSTA=0X90;
SPBRG=0X19;

while(RCIF==0);
c=RCREG;
RCIF=0;
if(c=='*')
while(1)
{
while(RCIF==0);
c=RCREG;
RCIF=0;
if(c=='#')
break;
TXREG=c;
while(TXIF==0);
TXIF=0;
}
}

USART ASYNCHRONOUS RECEPTION (PIC16F877A)


#include<pic.h>
void cmd(int);
void data(char);
void delay();
void main()
{
char c;
TRISB=0X00;
TRISC=0X80;
TXSTA=0X24;
RCSTA=0X90;
SPBRG=0X19;

cmd(0X38);
cmd(0X01);
cmd(0X06);
cmd(0X0E);
cmd(0X80);

while(1)
{
while(RCIF==0);
{
c=RCREG;
data(c);
RCIF=0;
}
}
}
void cmd(int a)
{
PORTB=a;
RC0=0;
RC1=0;
RC2=1;
delay();
RC2=0;
}

void data(char s)
{
PORTB=s;
RC0=1;
RC1=0;
RC2=1
delay();
RC2=0;
}

void delay()
{
int i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++);
}

Thursday, 24 September 2015

USART ASYNCHRONOUS TRANSMITTER (PIC16F877A)





#include<pic.h>
void main()
{

TRISC=0X80;;
TXSTA=0X24;
RCSTA=0X90;
SPBRG=0X19;

TXREG='A';
while(TXIF==0);
TXIF=0;
while(1);

}

PIC16F877A : USART programing(Registers and its functions)








Password matching in ATMEGA32L



#include <avr/io.h>
#include <util/delay.h>
void cmd(int);
void data(char);
char keypad();
void data1(char);
void display(const char*p)
{
while (*p!='\0')
{
data1(*p);
p++;
}
}
int main(void)
{char d;
int i=0,f=0;
char a[]="1234",S[30];            // predefined password is 1234        
DDRA=0xff;
DDRB=0xff;
DDRC=0xf0;
cmd(0x38);
cmd(0x01);
cmd(0x0e);
cmd(0x06);
cmd(0x80);
    while(1)
    {i=0;
      
 
 while(i<4)
  {
 d=keypad();
 if (d!='z')
 {
 S[i]=d;
 data(S[i]);
 i++;
     }
 
     }
 cmd(0x01);
 cmd(0x80);
for (i=0;i<4;i++)
if(S[i]!=a[i])
f=1;
if(f==1)
display("access denied");
else
display("access granted");
_delay_ms(5000);
cmd(0x01);
cmd(0x80);
}
}


char keypad()
    {
int e;
PORTC=0x7F;
e=PINC&0x0F;
switch (e)
{
case 0x07:
   return '1';
break;
case 0x0B:
return '2';
break;
case 0x0D:
return '3';
break;
case 0x0E:
return '4';
break;
}
PORTC=0xbf;
e=PINC&0x0f;
switch (e)
{
case 0x07:
   return '5';
break;
case 0x0b:
return '6';
break;
case 0x0d:
return '7';
break;
case 0x0e:
return '8';
break;
}
PORTC=0xdf;
e=PINC&0x0f;
switch (e)
{
case 0x07:
   return '9';
break;
case 0x0b:
return '0';
break;
case 0x0d:
return 'a';
break;
case 0x0e:
return 'b';
break;
}
PORTC=0xef;
e=PINC&0x0f;
switch (e)
{
case 0x07:
   return 'c';
break;
case 0x0b:
return 'd';
break;
case 0x0d:
return 'e';
break;
case 0x0e:
return 'f';
break;
default:
return 'z';
break;
}
    }
void cmd(int a)
{
PORTB=a;
PORTA=0x00;
PORTA=0x02;
_delay_ms(200);
PORTA=0x00;
}
void data(char s)
{
PORTB=s;
PORTA=0x03;
_delay_ms(1000);
PORTA=0x01;
}
void data1(char s)
{
PORTB=s;
PORTA=0x03;
_delay_ms(100);
PORTA=0x01;
}

Tuesday, 22 September 2015

4x4 Keypad programming in ARM7(LPC2129/38)( text on LCD(using 4 data lines) according to buttons pressed )



#include<lpc21xx.h>
void delay();
void cmd(int);
void cmd1(int);
void data(char);
char keypad();
int main()
{
char k;
IO0DIR=0XFFFFC0FF;
IO1DIR=0XFFFFFFFF;
cmd(0x30);
cmd(0x30);
cmd(0x30);
cmd(0x20);
cmd1(0x28);
cmd1(0x01);
cmd1(0x06);
cmd1(0x0e);
cmd1(0x80);
while(1)
{
k=keypad();
if(k!='z')
data(k);
}
}
char keypad()
{
int e;
IO0CLR=0XFFFFFFFF;
IO0SET=0X0001C000;
e=IO0PIN&0X00003C00;
switch(e)
{
case 0x00001C00:
return '1';
break;
case 0x00002C00:
return '2';
break;
case 0x00003400:
return '3';
break;
case 0x00003800:
return '4';
break;
}

IO0CLR=0XFFFFFFFF;
IO0SET=0X0002C000;
e=IO0PIN&0X00003C00;
switch(e)
{
case 0x00001C00:
return '5';
break;
case 0x00002C00:
return '6';
break;
case 0x00003400:
return '7';
break;
case 0x00003800:
return '8';
break;
}

IO0CLR=0XFFFFFFFF;
IO0SET=0X00034000;
e=IO0PIN&0X00003C00;
switch(e)
{
case 0x00001C00:
return '9';
break;
case 0x00002C00:
return '0';
break;
case 0x00003400:
return 'a';
break;
case 0x00003800:
return 'b';
break;
}

IO0CLR=0XFFFFFFFF;
IO0SET=0X00038000;
e=IO0PIN&0X00003C00;
switch(e)
{
case 0x00001C00:
return 'c';
break;
case 0x00002C00:
return 'd';
break;
case 0x00003400:
return 'e';
break;
case 0x00003800:
return 'f';
break;

default:
return 'z';
break;
}
}

 void cmd(int b)
{
unsigned int s;
s=b&0xf0;
IO1SET=s<<16;
IO1CLR=0X000F0000;
IO1SET|=0X00080000;
delay();
IO1CLR=0X00080000;
IO1CLR=0XFFFFFFFF;
}

void cmd1(int a)
{
unsigned int s;
s=a&0xf0;
IO1SET=s<<16;
IO1CLR=0X000F0000;
IO1SET|=0X00080000;
delay();
IO1CLR=0X00080000;
IO1CLR=0XFFFFFFFF;


s=a&0x0f;
IO1SET=s<<20;
IO1CLR=0X000F0000;
IO1SET|=0X00080000;
delay();
IO1CLR=0X00080000;
delay();
IO1CLR=0XFFFFFFFF;
}

void data(char a)
{
char s;
s=a&0xf0;
IO1SET=s<<16;
IO1CLR=0X000F0000;
IO1SET|=0X000A0000;
delay();
IO1CLR=0X00080000;
IO1CLR=0XFFFFFFFF;


s=a&0x0f;
IO1SET=s<<20;
IO1CLR=0X000F0000;
IO1SET|=0X000A0000;
delay();
IO1CLR=0X00080000;
delay();
IO1CLR=0XFFFFFFFF;
}

void delay()
{
int i;
for(i=0;i<500000;i++);
}