Pages

Monday 23 March 2015

Programming with PIC Microcontroller: Chapter-2

MPLAB IDE:
MPLAB IDE is a free integrated toolset for the development of embedded application on microchip IC and dsPIC microcontroller.


Install MPLAB by following the instructions sets provided in your software.


Creating a new project:
1) Open MPLAB
2) Create a folder in any drive.
3) Select project->project wizard


4.Click on next


5.Select PIC16F7877A then click on next.



6.Select HI-TECH Universal Tool Suite and click next










 

7.Click on browse and select the folder you saved on the drive and write a filename ex: lcd12.


 8.Click on save




9.Click on next->next->next->finish




10.You will get the following window.


11. Click on file->new->type a program



 12.Click on save->save it in the same folder with .c extension and click on save.


 13.Right click on source file ->add files->select your .c file->click on open.




14.click on programmer->select programmer->9PICkit 2

15.Click on configure ->configuration bits->unclick the configuration bits set in code->click ok select low voltage programming->then click the configuration set in code






16.Click on programmer->connect


17.Click on compile



Programming with PIC Microcontroller: Chapter-1

INTRODUCTION

EMBEDDED SYSTEMS

PIC16F877A

Overview:

The PIC 16F877A PIC microcontroller is one of the most popular general purpose microcontrollers. It is of 8-bit which means the most available operations are limited to 8-bits.It is a 40-pin IC.


Ports:

There is one 6-bit ports: A , 3 8-bit ports: B ,C,D and one 3 bit port:E.

PORTA (Pin 2 to 7)and TRISA register :

PORTA is a 6-bit wide, bidirectional port. The corresponding data direction register is TRISA. Setting a TRISA bit ( = 1) will make the corresponding. PORTA pin an input (i.e., put the corresponding output driver in a High-Impedance mode).Clearing a TRISAbit (= 0) will make the corresponding PORTA pin an output (i.e., put the contents of the output latch on the selected pin).Reading the PORTA register reads the status of the pins,whereas writing to it will write to the port latch.All write operations are read-modify write operations.Therefore, a write to a port implies that the port pins are read,the value is modified and then written to the port data latch.

PORTB(Pin 33 to 40)and TRISB register: 

PORTB is an 8-bit wide, bidirectional port. The corresponding data direction register is TRISB. Setting aTRISB bit (= 1)will make the corresponding PORTB pin an input(i.e., put the corresponding output driver in a High-Impedance mode). Clearing a TRISB bit (= 0)will make the corresponding PORTB pin an output (i.e.,put the contents of the output latch on the selected pin).Three pins of PORTB are multiplexed with the In-Circuit.Debugger and Low-Voltage Programming function:RB3/PGM,RB6/PGC and RB7/PGD.


PORTC(pin 15 to 18 and pin 24 to 26)and TRISC register:

PORTC is an 8-bit wide,bidirectional port. The corresponding data direction register is TRISC. Setting a TRISC bit (= 1) will make the corresponding PORTC pin an input (i.e., put the corresponding output driver in a High-Impedance mode). Clearing a TRISC bit (= 0)will make the corresponding PORTC pin an output (i.e.put the contents of the output latch on the selected pin).PORTC is multiplexed with several peripheral functions PORTC pins have Schmitt Trigger input buffers. When the I2C module is enabled, the PORTC<4:3>pins can be configured with normal I2C levels, or with SMBus levels, by using the CKE bit (SSPSTAT<6>).When enabling peripheral functions, care should be taken in defining TRIS bits for each PORTC pin. Some peripherals override the TRIS  bit to make a pin an output, while other peripherals override the TRIS bit to make a pin an input.Since the TRIS bit override is in effect while the peripheral is enabled, read-modify writeinstructions (BSF, BCF, XORWF) with TRISC as the destination, should be avoided. The user should refer to corresponding peripheral section for the correct TRIS bit settings.



PORTD(Pin 19to22 and pin 27to30)and TRISD register:
 
PORTD is an 8-bit port with Schmitt Trigger input buffers. Each pin is individually configurable as an input or output.PORTD can be configured as an 8-bit wide microprocessor port (Parallel Slave Port) by setting control bit, PSPMODE (TRISE<4>). In this mode, the input buffers are TTL.

PORTE(Pin8 to 10)and TRISE register: 

 
PORTE has three pins (RE0/RD/AN5,RE1/WR/AN6 and RE2/CS/AN7) which are individually configurable as inputs or outputs.These pins have Schmitt Trigger input buffers.The PORTE pins become the I/O control inputs for the microprocessor port when bit PSPMODE (TRISE<4>) is set. In this mode, the user must make certain that the TRISE<2:0> bits are set and that the pins are configured as digital inputs.Also, ensure that ADCON1 is configured for digital I/O. In this mode, the input buffers are TTL.PORTE pins are multiplexed with analog inputs. When selected for analog input, these pins will read as ‘0’s.

Thursday 19 March 2015

Programming With AVR Microcontroller: Chapter 4

SERIAL COMMUNICATION:

What is the USART?

The vast majority of devices in the current AVR family lineup contain a USART hardware subsystem. The USART hardware allows the AVR to transmit and receive data serially to and from other devices - such as a computer or another AVR.


The USART transmission system differs to most other digital busses in that it does not utilize a separate pin for the serial clock. An agreed clock rate is preset into both devices, which is then used to sample the Rx/Tx lines at regular intervals. Because of this, the USART requires only three wires for bi-directional communication (Rx, Tx and GND).

Setting up the Hardware


  • Connect your USART to a computer via the Com port.
  • You need to first identify which pins of your AVR(ATmega 16) are responsible for serial communication.
  • For ATmega 16 Pin 14 and Pin 15 are used for receiving and transmission.
  • Connect tx pin of usart to rx pin of ATmega 16 and rx pin of usart to the tx pin of the ATmega 16 .
  • Now to see the transmitted word back in pc you need to use HyperTerminal  (it’s a free software you need to download it)
  • set the Baud rate in the HyperTerminal
Setting up HyperTerminal
  1. open the HyperTerminal



2.Add name here I have added serial

    

3.Select your com port which you are using and press ok (to check the com port go to device manger and check in which com port is the usart connected)         















4.select the baud rate that you have set or just click Restore default it will set the default valve  and press ok

  
5.serial HyperTerminal will get open 



 

Initializing the USART
        

First off, you need to enable both the USART's transmission and reception circuitry. For the MEGA16, these are bits named RXEN and TXEN, and they are located in the control register UCSRB. When set, these two bits turn on the serial buffers to allow for serial communications

    Code:



int main (void)
{
UCSRB = (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
}

 Next, we need to tell the AVR what type of serial format we're using. Looking again at the MEGA16 datasheet, we can see that the bits responsible for the serial format are named UCSZ0 to UCSZ2, and are located in the USART control register C named UCSRC.

    Code:


UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes - URSEL bit set to select the UCRSC register

 
    The last thing to set for basic serial communications is the baud rate register. The baud rate register is 16-bit, split into two 8-bit registers as is the case with all 16-bit registers in the AVR device family. For this the baud valve need to be found for that we have a formula



BaudValue = (((F_CPU / (USART_BAUDRATE * 16))) - 1)
Where F_CPU is your AVR's system clock frequency (in Hz), and USART_BAUDRATE is the desired communication baud rate.
Given my example project using a system clock of 7372800Hz and a baud rate of 9600, our formula gives:

BaudValue = (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) BaudValue = (7372800 / (9600 * 16) - 1)
BaudValue = (7372800 / 153600 - 1)
BaudValue = (48 - 1)
BaudValue = 47
    This avoids "magic numbers" (unexplained constants) in our source code, and makes changing the baud rate later on very easy - just change the BAUD_RATE macro value. Now, we need to load this into the baud rate registers, named UBRRH (for the high byte) and UBRRL (for the low byte). This is simple via a simple bit shift to grab the upper eight bits of the BAUD_PRESCALE constant: 

UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register


Sending and receiving data
    We do this by the special register named UDR - short for "USART I/O Data Register”. On the MEGA16, the Transmission Complete flag is located in the control register UCSRA, and it is named TXC. Using this information we can construct a simple wait loop which will prevent data from being written to the UDR register until the current transmission is complete.



UDR = ByteToSend; // Send out the byte value in the variable "ByteToSend"
while ((UCSRA & (1 << TXC)) == 0) {}; // Do nothing until transmission complete flag set
 
    However this is non-optimal. We spend time waiting after each byte which could be better spent performing other tasks - better to check before a transmission to see if the UDR register is ready for data. We can do this by checking the USART Data Register Empty flag instead (called UDRE), also located in the UCSRA control register of the MEGA16
while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
UDR = ByteToSend; // Send out the byte value in the variable "ByteToSend"


    Now we can move on to receiving data. we need to check to see if we have received a byte.
To do this, we can check the USART Receive Complete (RXC) flag to see if it is set. Again, this is located in the UCSRA control register of the MEGA16:


while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR
ReceivedByte = UDR; // Fetch the received byte value into the variable "ReceivedByte"


CODE:

#include <avr/io.h>
#include <avr/delay.h>
#define    F_CPU ((unsigned long)8000000)
#define F_OSC 8000000// for 8mhz


#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / ( USART_BAUDRATE * 16UL))) - 1)

unsigned char

int main (void)
{
    char ReceivedByte;

    UCSRB = (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitry
    UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes

    UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
    UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register

    for (;;) // Loop forever
    {
        while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR
        ReceivedByte = UDR; // Fetch the received byte value into the variable                                          "ByteReceived"
        //_delay_ms(1000);

        while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
        UDR = ReceivedByte; // Echo back the received byte back to the computer
    }
}




Related Products: 

Atmega 16/32/64 Project Board                                      
                                                                                  

      
                                                               Product Code: G89S52     

Atmega 16/32/64 Development Board-USB 
                
                                                     Product Code: ATM-U

Atmega Programmer-USB  
         
                                                             Product Code: AVR     

ISP Atmel Programmer 

   

                                                       Product Code: UAB