Interfacing MAX6952
#define max_sdi PIN_C5
#define max_sdo PIN_C4
#define max_clk PIN_C3
#define max_cs PIN_C2
const int8 user_font[120]= {0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C,
0x77,0x6B,0x5D,0x6B,0X77,
0x1C,0x22,0x7F,0x22,0x1C};
#define config_addr 0x04
#define font_addr 0x05
#define test_addr 0x07
#define plane01_addr 0x20
#define plane02_addr 0x21
#define plane03_addr 0x22
#define plane04_addr 0x23
#define plane11_addr 0x40
#define plane12_addr 0x41
#define plane13_addr 0x42
#define plane14_addr 0x43
int8 config_byte;
void write_to_6952(int8 cmd,int8 data)
{
int8 i;
int16 ser_data;
ser_data=make16(cmd,data);
//shift_right(&ser_data,2,0);
output_low(max_clk);
output_low(max_cs);
for(i=1;i<=16;++i)
{
output_bit(max_sdi,shift_left(&ser_data,2,0));
output_high(max_clk);
delay_us(10);
// if(i<16)
output_low(max_clk);
//delay_us(6);
}
output_high(max_cs);
output_low(max_clk);
}
void write_user_font()
{
int8 j;
write_to_6952(font_addr,0x80);
for(j=0;j<120;j++)
{
write_to_6952(font_addr,user_font[j]);
}
}
/*******************************************************************
if state=1,normal operation
if state=0,shutdown
if wrt=1,write to 6952,otherwise update the config_byte register
*******************************************************************/
void shutdown(int1 state, int1 wrt)
{
if(state)
config_byte |= 0x01;
else
config_byte &= 0xFE;
if(wrt)
write_to_6952(config_addr,config_byte);
}
/*******************************************************************
if state=1,enable blinking
if state=0,disable blinking
if wrt=1,write to 6952,otherwise update the config_byte register
*******************************************************************/
void blink_enable(int1 state,int1 wrt )
{
if(state)
config_byte |= 8;
else
config_byte &= 0xF7;
if( wrt )
write_to_6952(config_addr,config_byte);
}
/*************************************************************************
if state=1,put the display in test mode
if state=0,normal mode
Does not affect plane data - original display is restored when set 0.
**************************************************************************/
void display_test(int1 state )
{
if(state)
write_to_6952(test_addr,1);
else
write_to_6952(test_addr,0);
}Interfacing ADS7807
#define sclk PIN_D3
#define sdata PIN_D4
#define rc PIN_D5
#define busy PIN_D6
#define cs PIN_C5
void init_ads7807()
{
output_low(sclk);
output_high(rc);
delay_us(2);
output_high(cs);
}
int32 read_ads7807()
{
BYTE i;
int32 adc_result=0;
while(!input(busy));
output_low(rc);
delay_cycles(1);
output_low(cs);
delay_us(1);
output_high(cs);
delay_cycles(1);
output_high(rc);
while(!input(busy));
output_low(cs);
for(i=1;i<=16;++i)
{
output_high(sclk);
shift_left(&adc_result,4,input(sdata));
output_low(sclk);
}
output_high(cs);
adc_result*=7629;
return adc_result/10000;
}PGA117 driver
#define READ 0x6A00
#define WRITE 0x2A00
#define NOP WRITE 0x0000
#define SDN_DIS 0xE100
#define SDN_EN 0xE1F1
#define GAIN_1 0
#define GAIN_2 1
#define GAIN_4 2
#define GAIN_8 3
#define GAIN_16 4
#define GAIN_32 5
#define GAIN_64 6
#define GAIN_128 7
#define CS BIT0
#define DI BIT1
#define DO BIT2
void write_pga(unsigned int value);
void set_ch_gain(unsigned int ch,unsigned int gain);
void config_spi();
void config_spi()
{
P1DIR |= 0x01; // P1.0 output
P3SEL |= 0x0C; // P3.2,3 USCI_B0 option select
P3DIR |= 0x01; // P3.0 output direction
UCB0CTL0 |= UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI mstr, MSB 1st
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 = 0x02;
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
void write_spi(char data)
{
UCB0TXBUF = data; // Byte to SPI TXBUF
while (!(IFG2 & UCB0TXIFG)); // USCI_A0 TX buffer ready?
}
void write_pga(unsigned int value)
{
P3OUT &= ~CS;
write_spi(value>>8);
write_spi(value);
P3OUT |= CS;
}
void set_ch_gain(unsigned int ch,unsigned int gain)
{
unsigned int command;
command = gain<<4;
command += ch;
command +=WRITE;
write_pga(command);
}LED Blinker Using a Timer/Counter
/**@file timer_blinker.c
@brief A more advanced LED blinker using a timer
@author Stephen Friederichs
@date 3/28/13
@note This code assumes that the LED is active high (pin sources current)
*/
/**@def F_CPU
@brief Clock frequency = 8MHZ - this is set by fuses and registers, not by this define
@note Always define this before including delay.h!
*/
#define F_CPU 8000000
/**@include io.h
@brief Include for AVR I/O register definitions
*/
#include <avr/io.h>
/**@include stdint.h
@brief Include for standard integer definitions (ie, uint8_t, int32_t, etc)
*/
#include <stdint.h>
/**@include delay.h
@brief Include for delay functions such as _delay_ms() and _delay_us()
*/
#include <util/delay.h>
/* Basic bit manipulation macros - everyone should use these. Please, steal these! Don't not use them and
don't rewrite them yourself!
*/
#define SET(x,y) x |= (1 << y)
#define CLEAR(x,y) x &= ~(1<< y)
#define READ(x,y) ((0x00 == ((x & (1<<y))>> y))?0x00:0x01)
#define TOGGLE(x,y) (x ^= (1<<y))
int main(void)
{
/*Initialization Code*/
/* ATMega328 Datasheet Table 14-1 Pg 78
Configure PD7 for use as Heartbeat LED
Set as Output Low (initially)
*/
SET(DDRD,7); //Direction: output
CLEAR(PORTD,7); //State: Lo
/* TCCR1A - ATMega328 Datasheet Section 16.11.1 pg 132
No waveform generation is required on this timer, so set all
ports to normal operation
*/
TCCR1A = 0x00;
/* TCCR1C - ATMega328 Datasheet Section 16.11.3 pg 135
This register is only used for output compare.
There's no output compare in this application so this can be all 0's
*/
TCCR1C = 0x00;
/* TCCR1B
Note: I've disabled the CKDIV8 fuse so that the clock source is 8MHz
ATMega328 Datasheet Section 16.11.2 pg 134 - TCCR1A
No input capture used - bits 7:6 are 0
No waveform generation used - bits 4:3 are 0
Clock source select is bits 2:0 but are not yet set - wait until the
main loop is ready to start
As per ATMega328 Datasheet Section 16.9.1 page 123, setting the timer
to Normal mode causes the counter to count up until it reaches 0xFFFF
at which point it will overrun and start back at 0. To configure this
timer/counter to produce a period of 500ms we need to start counting
at a value that causes it to reach 65535 in 500ms.
What is that value?
With a clock prescaler of 256 each count of the timer is roughly
(1/8MHz)*256 = 32uS
500ms / 32us /tick = 15625 ticks /500ms
The counter counts up to 65535, so to determine what value we have to
start at we subtract 15635 from 65536:
65536-15625 = 49910
*/
#define TIMER1_PERIOD 49910
TCNT1 = TIMER1_PERIOD;
/* Flash the LED for a second to show that initialization has successfully
occurred
*/
SET(PORTD,7);
_delay_ms(1000);
CLEAR(PORTD,7);
/* Start the timer/counter
ATMega328 Datasheet Section 16.11.2 Pg 135 - TCCR1B
No Waveform generation: bits 4:3 = 0
No input capture: bits 7:6 = 0
Clock select: ClkIO/256 - bits 2:0 = 100b = 0x04
*/
TCCR1B = 0x04; //This starts the counter/timer
while(1)
{
/* Handle the Heartbeat LED
When the timer/counter reaches 65535 the 500ms period will have
elapsed and TIFR1 bit 1 will be '1'
*/
if(READ(TIFR1,0))
{
/* ATMega328 Datasheet Section 16.11.9 pg137
Setting TIFR1 bit 1 clears the overflow flag
*/
SET(TIFR1,0);
/* Toggle the LED to flash it at 1Hz*/
TOGGLE(PORTD,7);
/* Reload the timer/counter count value to the previous value
so that the period remains the same
*/
TCNT1 = TIMER1_PERIOD;
}
/* Now you can do useful work here - no delay loops!*/
}
}Software UART receiver
/**
* @file
* Software serial (UART) receiver
*
* This module implements the receive engine for asynchronous serial
* communications using polling ("bit banging"). Transmission capability
* is not provided.
*
* The data format is <tt>8-N-1</tt>:
* - Eight data bits
* - No parity
* - One stop bit
*
* <h2>Structural overview</h2>
* The receiver is implemented as a polled finite state machine. The state
* of the I/O pin is passed as an argument to the state machine animation
* function <code>soft_uart_rx()</code>. The polling function must be called
* on a stable timebase at a frequency at least three times
* the bit rate. The function returns a flag to indicate that a character has
* been received and places the received character in a fixed buffer.
*
* <h2>Timing</h2>
* The baud rate of the transmitter constrains the ossortment of possible
* interrupt rates. However, this receiver is designed to be configurable so
* as to maximize those choices.
*
* Any frequency multiple of at least 3 is suitable. Is this example, the
* sample rate is four times the serial data bit rate:
*
* <pre>
* Given
* =====
* Baud rate specification: 1200 +/- 4%
* System interrupt rate: 5 kHz (200 us)
*
* Selecting a sample rate
* =======================
* Chosen multiplier: samples per bit
* Sample rate: 5 kHz / 4 == 1250 baud (4.16% high)
* </pre>
*
* Since the baud rate is high in this example, We will have a tendency to
* sample earlier and earlier on each successive bit. Therefore it is desirable
* to sample slightly later in the bit time if possible.
* <pre>
* \#define SOFT_SOFT_UART_RX_BIT_TIME 5
* \#define SOFT_UART_RX_START_SAMPLES 2
* </pre>
* The diagram below shows the resultant timing. The actual bit times are 4%
* slower, owing to the fact that the system interrupy is not an exact multiple
* of the bit time.
*
* The sample timing error at the stop bit is (4% X 9) = 36% too early.
* <pre>
* _______ _______________ _______________
* \\_______________/ \\...________________/
* +-------+---+---+---+---+---+---+---+---+...+---+---+---+---+---+---+---+---+
* | Cycle | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | 8 | 9 | A | B | C | D | E | F |
* +-------+---+---+---+---+---+---+---+---+...+---+---+---+---+---+---+---+---+
* | Data | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
* | | Start bit | Data bit 0 | | Data bit N | Stop bit |
* | Samp. | X | X | | | | | X | | | | | X | | | | X | |
* +-------+---+---+---+---+---+---+---+---+...+---+---+---+---+---+---+---+---+
* ^ ^ |<------------->|
* | | |
* | | SOFT_UART_RX_BIT_TIME -------+
* | |
* +---+---- SOFT_UART_RX_START_SAMPLES
* </pre>
* Here is an explanation of how a character is received:
* -# We sample the line continuously until the START (logic zero) bit is seen.
* -# Just to make sure it wasn't noise, we sample the line a second (or third
* or fourth, depending on the setting) time with the expectation that the
* state hasn't changed.
* -# We continue to sample the start bit until we have reached the center of
* the bit time. The line must stay in the low state. This shifts us to
* safety away from edges.
* -# We delay (frequency multiplier) cycles, ignoring the state of the line.
* This puts us in the middle of the first data bit.
* -# We sample and save the data bit, then wait (frequency multiplier - 1)
* cycles.
* -# We repeat until we have sampled all data (payload) bits. The last bit
* is sampled and must be a logic one.
*
* <h2>Limitations</h2>
* For speed, the receive buffer is implemented as a global variable that is
* to be accessed directly by the calling code. Also, the state variable
* is private to this module. Therefore, only one instance of the soft
* UART receiver is supported in a given project.
*
* @author Justin Dobbs
*/
#include <stdbool.h>
/** The number of times to sample the start bit.
This defines the phase shift of subsequent samples. If the interrupt rate is
a bit high relative to the baud rate, we want to sample late to
minimize cumulative timing error. */
#define SOFT_UART_RX_START_SAMPLES 3
/** The inter-bit delay time, a.k.a. the frequency multiplier */
#define SOFT_UART_RX_BIT_TIME 4
/* State definitions */
static bool st_idle (bool);
static bool st_start_bit (bool);
static bool st_delay_rx0 (bool);
static bool st_delay_rx1 (bool);
static bool st_delay_rx2 (bool);
static bool st_delay_rx3 (bool);
static bool st_delay_rx4 (bool);
static bool st_delay_rx5 (bool);
static bool st_delay_rx6 (bool);
static bool st_delay_rx7 (bool);
static bool st_delay_stop (bool);
static bool st_abort_wait_for_idle (bool);
/**
* Soft UART receiver polling function.
*
* This function implements the receiver. It should be called on a stable
* timebase at a fixed multiple of the bit rate.
*
* @note This is implemented as a pointer to a function to handle the current
* state. The caller need only invoke the function using the pointer.
*
* @param[in] x the state of the input line:
* - <code>true</code>: the line is high
* - <code>false</code>: the line is low
*
* @retval true if a character is ready in <code>soft_uart_rx_buf</code>
* @retval false otherwise
*/
bool (*soft_uart_rx)(bool) = st_idle;
/** Serial recieve buffer. This should be immediately read after
<code>soft_uart_rx()</code> returns <code>true</code>. */
unsigned char soft_uart_rx_buf;
/** Cycle counter, for timing. */
static unsigned char i;
/**
* Sampling continuously, waiting for the start bit.
*/
static bool st_idle (bool x)
{
if (!x)
{
i = SOFT_UART_RX_START_SAMPLES - 1;
soft_uart_rx = st_start_bit;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Sampling the start bit a few more times to make sure it's solid. This also
* provides time offset for sampling future bits in the middle of the bit time.
*/
static bool st_start_bit (bool x)
{
/* Reject if the start bit does not last long enough */
if (x)
{
soft_uart_rx = st_idle;
}
else if (--i == 0)
{
i = SOFT_UART_RX_BIT_TIME;
soft_uart_rx_buf = 0;
soft_uart_rx = st_delay_rx0;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Waiting one bit time, then sampling the LSb (bit 0).
*/
static bool st_delay_rx0 (bool x)
{
/* When it's time, shift in the data to the RX buffer. If we have
received all the data, go wait for the STOP bit. */
if (--i == 0)
{
if (x)
{
soft_uart_rx_buf |= 0x01;
}
i = SOFT_UART_RX_BIT_TIME;
soft_uart_rx = st_delay_rx1;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Waiting one bit time, then sampling bit 1.
*/
static bool st_delay_rx1 (bool x)
{
if (--i == 0)
{
if (x)
{
soft_uart_rx_buf |= 0x02;
}
i = SOFT_UART_RX_BIT_TIME;
soft_uart_rx = st_delay_rx2;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Waiting one bit time, then sampling bit 2.
*/
static bool st_delay_rx2 (bool x)
{
if (--i == 0)
{
if (x)
{
soft_uart_rx_buf |= 0x04;
}
i = SOFT_UART_RX_BIT_TIME;
soft_uart_rx = st_delay_rx3;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Waiting one bit time, then sampling bit 3.
*/
static bool st_delay_rx3 (bool x)
{
if (--i == 0)
{
if (x)
{
soft_uart_rx_buf |= 0x08;
}
i = SOFT_UART_RX_BIT_TIME;
soft_uart_rx = st_delay_rx4;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Waiting one bit time, then sampling bit 4.
*/
static bool st_delay_rx4 (bool x)
{
if (--i == 0)
{
if (x)
{
soft_uart_rx_buf |= 0x10;
}
i = SOFT_UART_RX_BIT_TIME;
soft_uart_rx = st_delay_rx5;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Waiting one bit time, then sampling bit 5.
*/
static bool st_delay_rx5 (bool x)
{
if (--i == 0)
{
if (x)
{
soft_uart_rx_buf |= 0x20;
}
i = SOFT_UART_RX_BIT_TIME;
soft_uart_rx = st_delay_rx6;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Waiting one bit time, then sampling bit 6.
*/
static bool st_delay_rx6 (bool x)
{
if (--i == 0)
{
if (x)
{
soft_uart_rx_buf |= 0x40;
}
i = SOFT_UART_RX_BIT_TIME;
soft_uart_rx = st_delay_rx7;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Waiting one bit time, then sampling bit 7.
*/
static bool st_delay_rx7 (bool x)
{
if (--i == 0)
{
if (x)
{
soft_uart_rx_buf |= 0x80;
}
i = SOFT_UART_RX_BIT_TIME;
soft_uart_rx = st_delay_stop;
}
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Waiting one bit time, then sampling the stop bit.
* @note The reception is aborted if the stop bit does not arrive on schedule.
*/
static bool st_delay_stop (bool x)
{
if (--i == 0)
{
/* STOP bit is always logic ONE by definition */
if (x)
{
soft_uart_rx = st_idle;
return true; /* Got a character */
}
else
{
/* Stop bit didn't happen when we expected it. Go sit and wait
indefinitely for the line to go high. */
soft_uart_rx = st_abort_wait_for_idle;
return false;
}
}
/* Haven't sampled the stop bit yet! */
return false;
}
/* -------------------------------------------------------------------------- */
/**
* Reception aborted; waiting as long as required for the line to idle high
* again.
*/
static bool st_abort_wait_for_idle (bool x)
{
/* NOW the line is finally high/idle again. Start the receive process over.
We did not get a character. */
if (x)
{
soft_uart_rx = st_idle;
}
return false;
}
/* Header file */
#if !defined(_SOFT_UART_RX_H)
#define _SOFT_UART_RX_H
/**
* @file
* Soft UART receiver header file
*
* This file implements the interface to the software UART reciever module.
* The full documentation is located in @ref soft_uart_rx.c.
*
* @author Justin Dobbs
*/
#include <stdbool.h>
/* Actually a function pointer, but this is supposed to be opaque. This is
called from a periodic interrupt.
@param[in] x the state of the serial line (true == high) */
extern bool (*soft_uart_rx) (bool x);
/* The receive buffer */
extern unsigned char soft_uart_rx_buf;
#endifIncremental Encoder Decode Trick
// 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++;CRC-16 Calculation
/***** crc16.h *****/
//Tested
#define CRC16_DNP 0x3D65 // DNP, IEC 870, M-BUS, wM-BUS, ...
#define CRC16_CCITT 0x1021 // X.25, V.41, HDLC FCS, Bluetooth, ...
//Other polynoms not tested
#define CRC16_IBM 0x8005 // ModBus, USB, Bisync, CRC-16, CRC-16-ANSI, ...
#define CRC16_T10_DIF 0x8BB7 // SCSI DIF
#define CRC16_DECT 0x0589 // Cordeless Telephones
#define CRC16_ARINC 0xA02B // ACARS Aplications
#define POLYNOM CRC16_XXX // Define the used polynom from one of the aboves
// It calculates the new crc16 with the newByte. Variable crcValue is the actual or initial value (0).
unsigned int crc16(unsigned int crcValue, unsigned char newByte);
/***** crc16.c *****/
#include "crc16.h"
unsigned int crc16(unsigned int crcValue, unsigned char newByte)
{
unsigned char i;
for (i = 0; i < 8; i++) {
if (((crcValue & 0x8000) >> 8) ^ (newByte & 0x80)){
crcValue = (crcValue << 1) ^ POLYNOM;
}else{
crcValue = (crcValue << 1);
}
newByte <<= 1;
}
return crcValue;
}
/***** EXAMPLE *****/
unsigned int exampleOfUseCRC16 (unsigned char *Data, usigned char len){
unsigned int crc;
unsigned char aux = 0;
crc = 0x0000; //Initialization of crc to 0x0000 for DNP
//crc = 0xFFFF; //Initialization of crc to 0xFFFF for CCITT
while (aux < len){
crc = crc16(crc,Data[aux]);
aux++;
}
return (~crc); //The crc value for DNP it is obtained by NOT operation
//return crc; //The crc value for CCITT
}DS18B20 Interfacing
float get_temp(void);
void reset_18B20(void);
void send_18B20(char data);
unsigned int read_18B20(void);
#define ONE_WIRE_PIN BIT4
#define ONE_WIRE_IN P1IN
#define ONE_WIRE_OUT P1OUT
#define ONE_WIRE_DIR P1DIR
float get_temp(void)
{
unsigned int temp;
reset_18B20();
send_18B20(0xcc); //send CCH,Skip ROM command
send_18B20(0x44);
delay_us(100);
reset_18B20();
send_18B20(0xcc); //send CCH,Skip ROM command
send_18B20(0xbe);
temp = read_18B20();
return((float)temp/8.0);
}
void reset_18B20(void)
{
ONE_WIRE_DIR |=ONE_WIRE_PIN;
ONE_WIRE_OUT &= ~ONE_WIRE_PIN;
__delay_cycles(500);
ONE_WIRE_OUT |=ONE_WIRE_PIN;
ONE_WIRE_DIR &= ~ONE_WIRE_PIN;
__delay_cycles(500);
}
void send_18B20(char data)
{
char i;
for(i=8;i>0;i--)
{
ONE_WIRE_DIR |=ONE_WIRE_PIN;
ONE_WIRE_OUT &= ~ONE_WIRE_PIN;
__delay_cycles(2);
if(data & 0x01)
{
ONE_WIRE_OUT |= ONE_WIRE_PIN;
}
__delay_cycles(60);
ONE_WIRE_OUT |= ONE_WIRE_PIN;
ONE_WIRE_DIR &= ~ONE_WIRE_PIN;
data >>=1;
}
}
unsigned int read_18B20()
{
char i;
unsigned int data=0;
for(i=16;i>0;i--)
{
ONE_WIRE_DIR |= ONE_WIRE_PIN;
ONE_WIRE_OUT &= ~ONE_WIRE_PIN;
__delay_cycles(2);
ONE_WIRE_OUT |=ONE_WIRE_PIN;
ONE_WIRE_DIR &= ~ONE_WIRE_PIN;
__delay_cycles(8);
if(ONE_WIRE_IN & ONE_WIRE_PIN)
{
data |=0x8000;
}
data>>=1;
__delay_cycles(120);
}
return(data);
}Sine Wave Generator
/*************************************************************************************************************
sineWaveGeneration
*************************************************************************************************************/
void sineWaveGeneration(unsigned long amplitude, unsigned long frequency, unsigned long time)
{
const double angularfrequency = (2.0 * pi) * (double)frequency;
const double pi = 3.141592654;
double timeperiod;
const double samplingrate = 48000;
double displacement;
//declare values
//obtain omega(the angular frequency)
// creates a 'for' loop that continues until 'endtime'
for (timeperiod = 0; timeperiod <= (double)time; timeperiod = timeperiod+(1/samplingrate))
{
//creates the sine wave A*sin(wt) by plotting every sample.
displacement = sin(angularfrequency * timeperiod) * amplitude;
}
return;
}Circular FIFO Buffer
/***** 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);
}
}






