Skip to main content

I/o port pic microcontroller programming using miKroC and CSS

PIC I/O Port Programming  (PIC18F4580)

we discussed in my last post that All digital I/O’s are grouped together into “Ports”, called A, B ,D. Here GPIO means “general purpose input output”. The PIC18F4580 features a total of 36 I/O-pins, called PORTA to PORTD. But there’s more. The device comes with so many additional internal units, that not all features could be connected to dedicated pins of the device package at any one time. The solution is: multiplex. This means, one single physical pin of the device can be used for different functions and it is up to the programmer to decide which function is selected.

Lets dive deep to grasp the some operations which we can perform with I/O PORTS.   



Each port has 3 registers for its operation:
• TRIS register (Data Direction register)
• PORT register (reads the logic levels on the pins of the device)
• LAT register often known as  (output latch)

The data latch (LAT register) is helpful for read-modify write
Operations on the worth that the I/O pins are driving.


TRIS register job:

Each of Ports A-E within the PIC18F4580 are often used for input or output The TRISx SFR (special function Register) is employed for the aim of creating a given port associate input or output port.as an example, to form a port associate output, we tend to write 0s to the TRISx register.

In different words, set output  to any of the pins of the Port B, we tend to should initial place 0s in to the TRISB register to form it associate output port, then send the information to the PORT B SFR itself. It should be noted that unless we tend to activate the TRIS bit (set it to zero), the information won't go from the port register to the pins of PIC. They will be sitting within the SFR of Port B within the CPU. To make a port associate input port, we tend to should initial place one s into the TRISx register for that port.

The easiest way to remember setup to output  TRIS bits is that 0 and letter O, 1 and letter I.  
Programmer must  take care TRIS bits set to 1 if data is needed to brought in to the WREG register from the pins of MC. To get a good grip of how ports are used in PIC18F4580 implement following example and also watch simulation.


Consider bits in Port A and Port B. Lets write a  program that shows the count from 0 to FFH (0000 0000 to 1111 1111 in binary) on the PORTS.

Proteus Simulation  

watch simulation to get better idea what we are trying to do.






CSS Programming Code shows the count from 0 to FFH (0000 0000 to 1111 1111 in binary) on the PORTS. 

int counter = 0;
void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

for (;;)
{

delay_ms(100);
OUTPUT_B(counter++);

delay_ms(100);
OUTPUT_A(counter++);

}
}

miKroC Programming Code shows the count from 0 to FFH (0000 0000 to 1111 1111 in binary) on the PORTS. 

#define LED PORTB      //notice how we can define PORTB

void main() {
ADCON1 = 0xff; //Configure Analog Pin as a digital IO
CMCON  = 0;   // Disable Comparators

TRISB = 0;       //make PORTB as an Output
TRISA  = 0;       //make PORTA as an Output
PORTA = 0;
LED = 0 ;
for (;;)
{

delay_ms(100);
LED++;
delay_ms(100);
PORTA++;

}


}

Reading Input PIN From PIC

At this point , we have build concepts of TRISx register and how to access PORTS. Next, we try to read the PIN and take some decisions based on the logic level of the bit, if-else instructions are conditional statements and executed a portion of code if user defined condition is true.


They allow you to observe a pin and build a call to execute code  depending on whether it is 0 or 1. once more it should be noted that the if-else  are often used for any bit(must be bit addressable) of the register, including the I/O ports A, B, C, D, and so on.

Lets Write a C18 program to monitor bit RA0. If it is HIGH, send 55H to Port C; otherwise,
send AAH to Port C.

Proteus Simulation  

watch simulation to get better idea what we are trying to do.




miKroC Programming Code  to monitor bit RA0. If it is HIGH, send 55H to Port C; otherwise,send AAH to Port C. 

#define mybit PORTA.RA0     //notice how we can define bit
void main() {
ADCON1 = 0xff; //Configure Analog Pin as a digital IO
CMCON  = 0;   // Disable Comparators

TRISC = 0;       //make Port C as an Output
TRISA.RA0  = 1;       //make PIN RA0 as an Input

while(1)
{

  if (mybit==1)
    PORTC = 0x55;

  else
    PORTC = 0xAA;
}


}


CSS Programming Code to monitor bit RA0. If it is HIGH, send 55H to Port C; otherwise,send AAH to Port C. 



void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);


while(1)
{

  
  if (input(PIN_A0))
    OUTPUT_C(0x55);

  else
   OUTPUT_C(0xAA);



}



}

Confusion Between Reading input pins vs. LATx port

Dealing with digital circuits like MC ,programmers must take care when reading data PORT, there are instructions which only read the status of ports internally often known as LATx,we should build distinction between these two types of instrctions.   

1. The instructions reads the status of the input pin.
2. On the contrary, instructions that get the data from  internal latch of the LAT register.

We will understand this scenario by writing code and simulation.  



LATx Register Role in Reading a Port or Latch


Lets design a code and simulation to understand the behavior of above explanation.

Reading LATx for ports

void main() {
ADCON1 = 0xff; //Configure Analog Pin as a digital IO
CMCON  = 0;   // Disable Comparators


TRISC = 0;       //make Port C as an Output


TRISA.RA0  = 0;       //make PIN RA0 as an output

while(1)
{
  PORTA.RA0 = 0x01;
  PORTC.RC0 = LATA.RA0;

}

}

Friends for further learning about PIC18 checkout next. 

Comments

Popular posts from this blog

How to use SPI Debugger

  SPI Debugger Hi Friends, in this post, we will discuss how to use SPI Debugger available in proteus for serial peripheral interface (SPI) which is a built-in feature of PIC microcontroller to communicate and data exchange between PIC and other devices. SPI Debugger Introduction to SPI Guys, SPI can be considered as a programmable shift  register, The SPI module is a synchronous serial I/O port that shifts a serial bit stream of variable length and data rate between the PIC and other peripheral devices. Here “synchronous” means that the data transmission is synchronized to a clock signal. To avoid further delay, let's see some important connections and different configurations to effectively use SPI Debugger. How to use SPI Debugger in MASTER & SLAVE configuration Select the virtual instrument and from the instrument, list selects "SPI Debugger". SPI Debugger Insert two SPI Debugger on the working area, select any one of them,

PIC18f4580 Timer0 calculation using miKroC , CSS and proteus

PIC18f4580 TIMER PROGRAMMING The PIC18f4580 has four timers. they're named as Timers zero, one, two, and three. they will be used either as timers to come up with a time delay or as counters to count events happening outside the microcontroller. First, we see however Timers zero is employed to come up with time delays. Every timer wants a clock pulse to tick. The clock supply will be internal or external. If we have a tendency to use the inner clock supply, then 1/ fourth of the frequency of the crystal oscillator on the OSC1 and OSC2 pins (Fosc/4) is fed into the timer. Therefore, it's used for time delay generation and for that reason is termed a timer. By selecting the external clock choice, we have a tendency to feed pulses through one among the PIC18's pins: this is often known as a counter. GIF  taken from  https://exploreembedded.com Basic registers of the timer Majority of t timers in 18F are 16 bits wide. Because the PIC 18 has an 8- bit ar

Frequency Counter using Interrupts PIC18f4580 Project

Frequency Counter  using PIC18f458 0 Proje ct This POST describes the construction of small frequency counter with a cheap PIC18f4580 microcontroller with 16 x 2 LCD.   Prerequisites: PIC18F4580 TIMER Programming. PIC18F4580 COUNTER Programming. PIC18F4580 Interfacing with 16x2 LCD. PIC18F4580 Interrupts Programming  ( we will cover in this POST) Concept: Frequency  is the number of occurrences of a repeating event per unit time. in our case we will measure a number of clocks generated by clock source per unit time. In this project, LCD is used to display the frequency and PIC timer 1 to measure the input signal and Timer0 to generate an indication that one second has gone. System software utilizes Timer-1 in the 16-bit counter mode to count the input signal and overflows of the counter are added to provide the aggregate count in multiples of 65536. Totaling the existing value of the counter at the conclusion provides the full count.