Example codes

Zoltan Padrah edited this page Aug 22, 2012 · 1 revision
Clone this wiki locally

First we have to specify the microcontroller type this is done the statement P16F84. Configure PORTs as Input or Output by setting or resetting the bits in the TRIS register.

Table of Contents

Writting to I/O ports

We have already discussed about writting to I/O ports. Now let us see how this can be done using microbe. Write program in microbe to set and reset the PORTB,0 pin with an interval of 100 millisec.

 P16F84
 TRISB = 0
 PORTA = 0
 PORTB = 0
 setpin:
      PORTB.0 = high
      delay 100
      PORTB.0 = low
      delay 100
 goto setpin
 end

In above program we use GOTO statement here the label used is setpin delay 100 means Delay the process for 100 millisecond PORTB.0 = high means set the PORTB,0 as high. PORTB.0 = low means set the PORTB,0 as low.

Reading from I/O ports

Program to read the state of PORTA,0 pin and set PORTB,0 if high and vice versa. Set PORTA,0 as input , PORTB as output by TRISA=1 and TRISB=0 statements respectively. Clear the PORT’S by using the Statements PORTA=0 and PORTB=0. Test the PORTA,0 pin by if condition if high set PORTB,0 else PORTB,0 = low. Program is given below,

 P16F84
 TRISA = 1
 TRISB = 0
 PORTA = 0
 PORTB = 0
 testpin:
 if PORTA.0 is high then
 {
      PORTB.0 = high
 }
 else
 {
      PORTB.0 = low
 }
 goto testpin
 end

More Programs

Program to display the binary equilavent of a from Zero to Nine.

Select PORTB as output, program is given below

 P16F84
 TRISB = 0
 PORTB = 0
 forloop:
 for x = 1 to 9
 {
      PORTB = x
      delay 100
 }
 goto forloop
 end

Note : PORTB = x means write x to the PORTB.

Program to drive a D.C motor to left or right depending upon whether PORTA,0 is high or low

 P16F84
 TRISB = 4
 PORTB = 0
 testpin:
 if PORTA.0 is high then
 {
      PORTB.0 = high
      PORTB.1 = low
      goto testpin
 }
 else
 {
      PORTB.0 = low
      PORTB.1 = high
      goto testpin
 }
 end

Design a Counter using microcontroller

As in the case of flowcode hera also we select RA0 as input pin and PORTB as output port. Code is given below,

 P16F84
 count = 0
 x=0
 TRISB = 0
 PORTA = 0
 PORTB = 0
 testpin:
 if PORTA.0 is high then
 {
      if x == 0 then
      {
            count = count+1
            x=1
            PORTB = count
            if count == 8 then
            {
                  count = 0
            }
            goto testpin
      }
      else
      {
            goto testpin
      }
 }
 else
 {
      x=0
      goto testpin
 }
 end