Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

9 I2c-Spi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

Rotating Encoder,UART, SPI, I2C,

Parallel
Rotating Encoder
// DC motor control with PIC16F877A and rotary encoder CCS C
code.

#define SW PIN_B3 // Rotary encoder button pin connection


#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 20MHz)
#use fast_io(B)

int1 motor_direction;
int8 last_read;
signed int8 quad = 0, change;
signed int16 motor_speed = 0;
#INT_RB // RB port interrupt on change
void RB_IOC_ISR(void) {
int8 encoderRead;
clear_interrupt(INT_RB);
encoderRead = input_b() & 0x30;
if(encoderRead == last_read)
return;
if(bit_test(encoderRead, 4) == bit_test(last_read, 5))
quad -= 1;
else
quad += 1; 4-5

last_read = encoderRead;
}
signed int8 EncoderGet(void) {
signed int8 value = 0;
while(quad >= 4){
value += 1;
quad -= 4;
}
while(quad <= -4){
value -= 1;
quad += 4;
}
return value;
}
void main() { port_b_pullups(TRUE); enable_interrupts(GLOBAL);
clear_interrupt(INT_RB); setup_timer_2(T2_DIV_BY_16, 63, 1);
// Set PWM frequency to 4.88 KHz with 8-bit resolution
delay_ms(1000); last_read = input_b() & 0x30;
enable_interrupts(INT_RB);
while(TRUE) { delay_ms(50);
change = EncoderGet();
if(change){
motor_speed += change;
if(motor_speed > 255)
motor_speed = 255;
if(motor_speed < 0)
motor_speed = 0;
set_pwm1_duty(motor_speed); // Set pwm1 duty cycle
set_pwm2_duty(motor_speed); // Set pwm2 duty cycle
}
if(!input(SW)) {
while(!input(SW));
motor_direction = !motor_direction;
if(motor_direction) {
setup_ccp2(CCP_OFF);
output_low(PIN_C1);
setup_ccp1(CCP_PWM);
}
else {
setup_ccp1(CCP_OFF);
output_low(PIN_C2);
setup_ccp2(CCP_PWM);
}
}
}
}
PIC16 C UART Serial Link
The CCS C library functions
PIC16 C UART Serial Link

RS232 Peripheral Simulation


PIC16 C UART Serial Link
#include " 16F877A.h "
#use delay(clock= 8000000) // Delay function needed for RS232
#use rs232(UART1) // Select hardware UART
void main() //************************************
{
int incode;
setup_uart(9600); // Set baud rate
while(1)
{
incode = getc(); // Read character from UART
printf( " ASCII = %d " ,incode); // Display it on
putc(13); // New line on display
}
}
Pin RC6 (TX) and pin RC7 (RX) are used for the UART (serial) communication between the
microcontroller and the computer. To change between TTL logic levels (5V) and RS232
signals (+/-12V), an IC is needed which is max232
// PIC16F877A UART example with CCS C
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000)
#use rs232(uart1, baud = 9600) // Initialize UART module
char i;
void main(){
putc(13); // Go to first column
printf("Hello world!"); // UART write
delay_ms(5000); // Wait 5 seconds
putc(13); // Go to first column
putc(10); // Start a new line
printf("PIC16F877A UART example"); // UART Write
putc(13); // Go to first column
putc(10); // Start a new line
while(TRUE){
if(kbhit()){ // If data has been received
i = getc(); // UART read
putc(i); // Send it back
}
}
}
SPI Bus Interface
The SPI interface has four signal lines: MOSI, MISO, SCLK, and SS.
•MOSI (Master Output, Slave Input)
MOSI line is used to transfer data from master to slave.
•MISO (Master Input, Slave Output)
MISO line is used to transfer data from slave to master.
•SCLK (Serial Clock)
SCLK clock pulse output from the master and clock pulse input for the slave. The SCLK line is
used to synchronize data transfer between master and slave devices via MOSI and MISO
lines.

•SS (Slave Select)


The SPI master device uses this line to select
the SPI slave. SS is an active low line.
The data is transferred bit by bit, starting with
the high bit. The slave must be in high-
impedance state if the slave is not selected
through the SS line.
SPI Operation Modes
SPI Full Duplex Mode
In full duplex mode SPI master device simultaneously transmits data to a
slave and receives data from a slave. This way only a single slave device can
be engaged at one time. An SS line is used to select the particular SPI slave
device.

www.diolan.com
SPI Half Duplex Mode (Single Read)
In half duplex single read mode SPI master device only receives
data from a slave. This way only a single slave device can be
engaged at one time. An SS line is used to select the slave device.
SPI Half Duplex Mode (Single Write)

In half duplex single write mode SPI master device only transmits
data to a slave, and doesn't receive any data from the slave. This
way simultaneous operation with several slave devices is
possible.
SPI Serial Bus
SPI Function Set
Wired AND connection using diodes and a resistor
MSB is fixed for all devices. For Ex 1010EPROM.

The addresses of most I2C devices are fixed and documented in the respective datasheet. Some I2C devices allow a few bits
out of the 7 or 10 address bits to be configured by hardware values on pins labeled A0, A1, and A2 for example.
Test circuit with I2C
24AA256/24LC256/24FC256
I2C test system
I2C functions

I2C SERIAL PORT

I2C START Issue start command in master i2c_start();


mode
I2C WRITE Send a single byte i2c_write(outbyte);
I2C READ Read a received byte inbyte = i2c_read();
I2C STOP Issue a stop command in i2c_stop();
master mode
I2C POLL Check to see if byte received sbit = i2c_poll();
#include "16F877A.h"
#use delay(clock=4000000)
#use i2c(MASTER,SCL=PIN_C3,SDA=PIN_C4)

/*
void main() //Ghi 1 byte
{
int sendbyte, lowadd;

lowadd=0;
port_b_pullups(1);
sendbyte=(input_B());
i2c_start(); // start write cycle
i2c_write(0xA0); // send control byte
i2c_write(0x00); // send high address
i2c_write(lowadd); // send low address
i2c_write(sendbyte); // send data
i2c_stop();
delay_ms(5); // wait for write
while(1)
{
}
}
#include "16F877A.h"
#use delay(clock=4000000)
#use i2c(MASTER,SCL=PIN_C3,SDA=PIN_C4)
void main() //Ghi 5 bytes
{
int sendbyte, lowadd;
port_b_pullups(1);
sendbyte=(input_B());
for (lowadd=0; lowadd<5; lowadd++ )
{
i2c_start(); // start write cycle
i2c_write(0xA0); // send control byte
i2c_write(0x00); // send high address
i2c_write(lowadd); // send low address
i2c_write(sendbyte); // send data
i2c_stop();
delay_ms(5); // wait for write
}
while(1)
{
}
}
#include "16F877A.h"
#use delay(clock=4000000)
#use i2c(MASTER,SCL=PIN_C3,SDA=PIN_C4)
void main()//Ghi ra 256bytes
{
int sendbyte, lowadd;

lowadd=0;
port_b_pullups(1);
sendbyte=(input_B());

while(1)
{
i2c_start(); // start write cycle
i2c_write(0xA0); // send control byte
i2c_write(0x00); // send high address
i2c_write(lowadd); // send low address
i2c_write(sendbyte); // send data
i2c_stop();

delay_ms(5); // wait for write


lowadd++; // inc address
}
}
Parallel and Serial Interfaces

270R / 270 ohm Resistor

You might also like