#include<p16f877a.inc>
#define s_data_o PORTC,5 ;serial data out
#define s_data_i PORTC,4 ;serial data in
#define s_clock PORTC,3 ;clock out
udata_shr
tx_reg res 1
rx_reg res 1
code
;************************
;Configure I/O Ports
;Load data in WREG
;Call soft_spi_write
;************************
soft_spi_write
global soft_spi_write
banksel tx_reg
movwf tx_reg ;store W = tx_reg
banksel PORTC ;Bank 0
bsf STATUS,C ;Set Carry Flag=1
send_next_bit
rlf tx_reg,F ;rotate left
movf tx_reg,F ;Check wheter 8 bit transmitted or not
btfsc STATUS,Z ;If no ,send next bit
return ;if yes,return
bcf s_data_o ;data line low
btfsc STATUS,C ;check the bit in carry,
bsf s_data_o ;if high,s_data_o =1
fill (nop),3
bsf s_clock ;s_clock=1 | _
fill (nop),5 ; |clock high to low _| |_
bcf STATUS,C ;clear carry |
bcf s_clock ;S_clock=0 |
fill (nop),3
goto send_next_bit ; looping process...........
;**************************************************
;Configure I/O Ports
;Call soft_spi_read
;This fuction returns the received data is in WREG
;**************************************************
soft_spi_read ;subroutine for receive
global soft_spi_read
movlw 0x01 ;eight bit reception
movwf rx_reg
read_next_bit
rlf rx_reg,f ;rotating the rx_reg register to store the received bit
bsf s_clock
fill (nop),5
btfsc s_data_i
bsf rx_reg,0 ;receiving the data
bcf s_clock
fill (nop),3
btfss STATUS,C ;testing whether the reception is compleate or not
goto read_next_bit ;if not compleated do the process again
movf rx_reg,W ;restore data in WREG
return
end
%Script to load and plot 16-bit accelerometer data
printf "Running acceleration data analysis script\r\n"
clear * %Clear all variables
ts = (1/1000); %1KHz sampling rate
%Path to TXT file with accelerometer samples
accel_data_path = "accel_data.txt";
%Open the acceleration data file as read-only, binary mode
file_accel_data = fopen(accel_data_path,"rb");
%Read unit16 samples from TXT file into an array
%count is # of samples, val is array of values
[val,count] = fread(file_accel_data,Inf,"uint16");
fclose(file_accel_data);
%Generate a time vector from t=0 to the end determined by count and sampling time
tmax = (count-1)*ts;
t=0:ts:tmax;
%Open figure 1
figure(1)
%Plot accelerometer samples
plot(t,val','1')
%Make the plot look pretty
title("Raw Sampled Accelerometer Data")
xlabel("Time (s)")
ylabel("Accelerometer Data")
%Save the plot to disk
print("plots/raw_accel_data.png")
// Incremental encoder decoding.
uint new;
uint old;
bool direction;
int count=0;
/* Get the latest 2bit sample. */
new = get_new_bits(); // Your function to get new samples.
/* XOR the lower bit from the new sample with the upper bit from the old.*/
direction = (new&1) ^ (old>>1)
/* The "direction" var holds the direction of rotation. */
/* Transfer the new sample to the old sample. */
old = new;
/* We can use this to inc or dec a counter to maintain position of rotation. */
if(direction) counter--; else counter++;
/***** circularBuffer.h *****/
#ifndef CIRCULAR_BUFFER_H_
#define CIRCULAR_BUFFER_H_
#define BUFFER_SIZE 128
#define TYPE char
// Check if the buffer it is full
bool isFull();
// Check if the buffer it is empty
bool isEmpty();
// Get the first element from the FIFO queue
TYPE getElement();
// Add an element to the FIFO queue
bool addElement(TYPE data);
// Return the number of Elements that are in the FIFO queue
unsigned char getNumberOfElements();
#endif /*CIRCULAR_BUFFER_H_*/
/***** circularBuffer.cpp *****/
#include "circularBuffer.h"
TYPE Buffer[BUFFER_SIZE + 1]; // It needs 1 extra byte to difference full and empty
unsigned char next = 0;
unsigned char first = 0;
bool isFull(){
if (getNumberOfElements() == BUFFER_SIZE){
return true;
}else{
return false;
}
}
bool isEmpty(){
if ( next == first ){
return true;
}else{
return false;
}
}
TYPE getElement(){
TYPE theElement = 0;
if (! isEmpty()){
theElement = Buffer[first];
if ( first != BUFFER_SIZE ){
first++;
}else{
first = 0;
}
}
return theElement;// Return 0 always if it is empty, must be checked before
}
bool addElement(TYPE data){
if (!isFull()){
Buffer[next] = data;
if ( next != BUFFER_SIZE ){
next++;
}else{
next = 0;
}
return true;
}else{
return false;
}
}
unsigned char getNumberOfElements(){
if (next >= first){
return (next - first);
}else{
return (BUFFER_SIZE - next + first);
}
}