9 I2c-Spi
9 I2c-Spi
9 I2c-Spi
Parallel
Rotating Encoder
// DC motor control with PIC16F877A and rotary encoder CCS C
code.
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
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 1010EPROM.
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
/*
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();