
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;
}
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);
}
PID (Floating Point)
/*! \details This structure holds the data to run a
* floating point PID loop.
*/
typedef struct{
float max /*! \brief Max manipulated value */;
float min /*! \brief Miniumum manipulated value */;
float e /*! \brief Error value */;
float i /*! \brief Integrator value */;
float kp /*! \brief Proportional constant */;
float ki /*! \brief Integrator constant */;
float kd /*! \brief Differential constant */;
} pid_f_t;
/*! \details This function initializes the data in a PID structure.
*
*/
void pid_init_f(pid_f_t * ptr /*! A pointer to the PID data structure */,
float min /*! The manipulated variable's minimum value */,
float max /*! The manipulated variable's maximum value */){
memset(ptr, 0, sizeof(pid_f_t));
ptr->min = min;
ptr->max = max;
}
/*! \details This function updates the value of the manipulated variable (MV)
* based on the current state of the PID loop.
*/
float pid_update_f(float sp /*! The set point */,
float pv /*! The process variable */,
pid_f_t * ptr /*! A pointer to the PID constants */){
float temp;
float e;
float p;
float manp;
float tmpi;
//get the error from the last call
e = ptr->e;
//calculate the new error (set point - present value)
ptr->e = sp - pv;
//use a temp variable for the integrator
tmpi = ptr->i + ptr->e;
//update the manipulated process variable
manp = ptr->kp * ptr->e + ptr->ki * tmpi + ptr->kd * (ptr->e - e);
//the integrator is only updated if the manipulated process is within range
//otherwise the system will likely become unstable
if ( (manp < ptr->max) && (manp > ptr->min) ){
ptr->i = tmpi;
} else if ( manp > ptr->max ){
manp = ptr->max;
} else if ( manp < ptr->min ){
manp = ptr->min;
}
return manp;
}
soft Real Time Clock implementation
typedef struct t_smalltm{
u16 year;
u8 mon;
u8 day;
u8 hour;
u8 min;
u8 sec;
} t_smalltm;
t_smalltm rtc;
//**************************************************************
// call this function every 1 sec. from timer interrupt.
// for fast code execution rtc will be placed in internal RAM.
//**************************************************************
void realTimeClock(void)
{u8 rest;
if (++rtc.sec==60) // sec
{
rtc.sec=0;
if (++rtc.min==60) // min
{
rtc.min=0;
if (++rtc.hour==24) // hour
{
rtc.hour=0;
rtc.day++; // day
rest=rtc.year%4;
if ((((rest==0 && rtc.year%100!=0) || rtc.year%400==0)
&& rtc.mon==2 && rtc.day==30)
|| (rest && rtc.mon==2 && rtc.day==29)
|| ((rtc.mon==4 || rtc.mon==6 || rtc.mon==9 || rtc.mon==11)
&& rtc.day==31)
|| (rtc.day==32))
{
rtc.day=1;
if (++rtc.mon==13) // mon
rtc.mon=1, rtc.year++; // HAPPY NEW YEAR ! :)
}
}
}
}
}
//**************************************************************
// read RTC function
void getRTC(t_smalltm* stm)
{
disableInterrupts(); // evite erronated read
// because realTimeClock is called from interrupt
memcpy(stm,&rtc,sizeof(t_smalltm));
enableInterrupts();
}
//**************************************************************
// set RTC function
void setRTC(u16 year, u8 mon, u8 day, u8 hour, u8 min, u8 sec)
{
disableInterrupts();
rtc.year=year;
rtc.mon=mon;
rtc.day=day;
rtc.hour=hour;
rtc.min=min;
rtc.sec=sec;
enableInterrupts();
}
Interfacing SM630
#define cmd_add_fingerprint 0x40
#define cmd_search_fingerprint 0x44
#define cmd_packet 0x10
#define data_packet 0x20
#define res_packet 0x30
#define res_rcv_correct 0x01
#define res_rcv_error 0x02
#define res_opr_success 0x31
#define res_finger_detected 0x32
#define res_timeout 0x33
#define res_process_fail 0x34
#define res_para_error 0x35
#define res_fingerprint_found 0x39
#define res_fingerprint_unfound 0x3A
#use rs232(baud=57600,xmit=PIN_C6,rcv=PIN_C7,bits=8,parity=n,stop=1,stream=Finger,timeout=1000)//,force_sw)
int8 cmd_buffer[10],response_buffer[15];
int8 find_checksum(int8 total_byte)
{
int8 byte_count;
int16 check_sum=0;
for(byte_count=0;byte_count<total_byte;byte_count++)
{
check_sum+=cmd_buffer[byte_count];
}
return(make8(check_sum,0));
}
void cmd_to_sm630(int8 total_byte)
{
int8 byte_count;
for(byte_count=0;byte_count<total_byte;byte_count++)
{
fputc(cmd_buffer[byte_count],Finger);
delay_us(10);
}
}
void response_from_sm630(int8 total_byte)
{
int8 byte_count;
while(fgetc(Finger)!=0x4D);
response_buffer[0]=0x4D;
for(byte_count=1;byte_count<total_byte;byte_count++)
{
response_buffer[byte_count]=fgetc(Finger);
}
}
int8 add_finger(int16 finger_id)
{
cmd_buffer[0]=0x4D; //Packet Head
cmd_buffer[1]=0x58; //Packet Head
cmd_buffer[2]=cmd_packet; //Command Packet
cmd_buffer[3]=0x03; //3 byte length
cmd_buffer[4]=cmd_add_fingerprint; //Add finger Print cmd
cmd_buffer[5]=make8(finger_id,1);//Higher byte of finger print id
cmd_buffer[6]=make8(finger_id,0);//Lower byte of finger print id
cmd_buffer[7]=find_checksum(7);//Check sum of 7 bytes
cmd_to_sm630(8);
response_from_sm630(6); //Read 6 bytes
if(response_buffer[4] == res_rcv_correct)
{
response_from_sm630(7); //Read 6 bytes
if(response_buffer[5] == res_opr_success)
{
//Display Press finger again
//lcd_goto(2,1);
//lcd_send_byte(" Press again ");
response_from_sm630(7); //Read 6 bytes
}
}
return (response_buffer[5]);
}
int8 search_finger(int16& result_id,int16 num_fingerprint)
{
output_low(PIN_A5);
cmd_buffer[0]=0x4D; //Packet Head
cmd_buffer[1]=0x58; //Packet Head
cmd_buffer[2]=cmd_packet; //Command Packet
cmd_buffer[3]=0x05; //5 byte length
cmd_buffer[4]=cmd_search_fingerprint; //Search finger Print cmd
cmd_buffer[5]=0x00; //Higher byte of Starting id
cmd_buffer[6]=0x00; //Lower byte of Starting id
cmd_buffer[7]=make8(num_fingerprint,1);//Higher byte of number of fingerprints searched
cmd_buffer[8]=make8(num_fingerprint,0);//Lower byte of number of fingerprints searched
cmd_buffer[9]=find_checksum(9);//Check sum of 9 bytes
cmd_to_sm630(10);
response_from_sm630(6); //Read 6 bytes
if(response_buffer[4] == res_rcv_correct)
{
response_from_sm630(7); //Read 7 bytes
if(response_buffer[5] == res_opr_success)
{
delay_ms(10);
response_from_sm630(6); //Read 6 bytes
//disp_response(6);
if(response_buffer[5] == res_fingerprint_found)
{
response_buffer[0]=fgetc(Finger);
response_buffer[1]=fgetc(Finger);
result_id=make16(response_buffer[0],response_buffer[1]);
}
}
}
else
response_buffer[5]=response_buffer[4];
return (response_buffer[5]);
}
Playing sound in DAC using simple 8051 MCU
/*System reads wav files by SPI from external memory . Samples read are sent to external DAC by other SPI to be played. It's nedeed ISR reception (not included) of SPI to manage info of this file. At the beginnig, this timing is not important, but after all info of wav file is read, timming is fitted to sampling time of wav to play.
This Timer ISR manages read samples by SPI_Memory and write in SPI_DAC in 8051 MCU. Because this MCU has only one SPI,the info transmited to DAC is managed by simple GPIOs */
void Timer2_ISR (void) interrupt 5
{
char desp=0;
char temp=0;
SFRPAGE_save = SFRPAGE;
//! buffer to store 1 or 0 in bit content audio to convert
if(TF2H == 1)
{
/*When system starts, only reads from memory, to get wav header (sampletime, length, and because
there are more than one wav file in memory, the starting address of wav, In "configurating" the
timer period is not sampleTime*/
if (configurating==TRUE)
{
SPI_Read_Memory(); /*it's nedeed ISR to manage recepcion*/
}
else
{
/*pread counts samples of wav file reading*/
if (pread<(audioLength+1))
{
/*wavSampleRead is the last sample stored by SPI, taken in SPI ISR*/
PCA0CPH0=wavSampleRead;
desp=4;
MOSI_SPI_SW=0;
index++;
if (index==256)
{
index=0;
}
CS_DAC=0; /*Chip Select of DAC. This SPI is controlled by GPIOs*/
while (desp!=0)
{
CLK_SPI_SW=0;
CLK_SPI_SW=1;
desp--;
}
desp=0;
while (desp!=8)
{
CLK_SPI_SW=0;
if ((wavSampleRead&0x80)!=0)
{
MOSI_SPI_SW=1;
}
else
{
MOSI_SPI_SW=0;
}
CLK_SPI_SW=1;
desp++;
wavSampleRead=wavSampleRead<<1;
}
CLK_SPI_SW=0;
MOSI_SPI_SW=0;
CLK_SPI_SW=1;
CLK_SPI_SW=0;
CLK_SPI_SW=1;
CLK_SPI_SW=0;
CLK_SPI_SW=1;
CLK_SPI_SW=0;
CLK_SPI_SW=1;
CLK_SPI_SW=0;
CS_DAC=1;
MOSI_SPI_SW=0;
/* starts new transmision on SPI to read new sample from memory*/
SFRPAGE = ACTIVE_PAGE;
SPI_transferData = NO_OK;
SPI0DAT = TRANSFER_MEMORY_COMMAND;
SFRPAGE = SFRPAGE_save;
buffer_Index=0;
pread++;
}
else
{
/*in configurating mode is stored a buffer with some samples. This buffer
it's used to allow "clac" noises to reproduce the audioagain (becuase the audio is
playing periodically. The audios are siren sounds, and the last sample must linked whit
the first sample, having a compled wave form)*/
PCA0CPH0=buffer[buffer_Index];
temp=buffer[buffer_Index];
buffer_Index++;
if (buffer_Index==1)
{
looping=1;
}
else if (buffer_Index==BUFFER_SIZE)
{
pread=BUFFER_SIZE+1;
}
desp=4;
MOSI_SPI_SW=0;
CS_DAC=0;
while (desp!=0)
{
CLK_SPI_SW=0;
CLK_SPI_SW=1;
desp--;
}
desp=0;
while (desp!=8)
{
CLK_SPI_SW=0;
if ((temp&0x80)!=0)
{
MOSI_SPI_SW=1;
}
else
{
MOSI_SPI_SW=0;
}
//CLK_SPI_SW=0;
CLK_SPI_SW=1;
desp++;
temp=temp<<1;
}
desp=4;
while (desp!=0)
{
CLK_SPI_SW=0;
MOSI_SPI_SW=0;
//CLK_SPI_SW=0;
CLK_SPI_SW=1;
desp--;
}
CLK_SPI_SW=0;
CS_DAC=1;
MOSI_SPI_SW=0;
CS_DAC=1;
}
if (pread==BUFFER_SIZE+1)
{
/*First sample to begin the cicle*/
SFRPAGE = ACTIVE_PAGE;
SPI_transferData = NO_OK;
SPI0DAT = TRANSFER_MEMORY_COMMAND;
SFRPAGE = SFRPAGE_save;
}
}
}
TF2H = 0;
}
}
LCD, 4 bit data mode
/* note: lcd model MC21605A6W */
/* note: DB0:3 and RW must be grounded */
/* note: see https://github.com/texane/lcmeter for usage */
#include <stdint.h>
#include <avr/io.h>
#define LCD_POS_DB 0x02
#define LCD_PORT_DB PORTD
#define LCD_DIR_DB DDRD
#define LCD_MASK_DB (0x0f << LCD_POS_DB)
#define LCD_POS_EN 0x06
#define LCD_PORT_EN PORTD
#define LCD_DIR_EN DDRD
#define LCD_MASK_EN (0x01 << LCD_POS_EN)
#define LCD_POS_RS 0x07
#define LCD_PORT_RS PORTD
#define LCD_DIR_RS DDRD
#define LCD_MASK_RS (0x01 << LCD_POS_RS)
static inline void wait_50_ns(void)
{
__asm__ __volatile__ ("nop\n\t");
}
static inline void wait_500_ns(void)
{
/* 8 cycles at 16mhz */
__asm__ __volatile__ ("nop\n\t");
__asm__ __volatile__ ("nop\n\t");
__asm__ __volatile__ ("nop\n\t");
__asm__ __volatile__ ("nop\n\t");
__asm__ __volatile__ ("nop\n\t");
__asm__ __volatile__ ("nop\n\t");
__asm__ __volatile__ ("nop\n\t");
__asm__ __volatile__ ("nop\n\t");
}
static inline void wait_50_us(void)
{
/* 800 cycles at 16mhz */
uint8_t x;
for (x = 0; x < 100; ++x) wait_500_ns();
}
static inline void wait_2_ms(void)
{
wait_50_us();
wait_50_us();
wait_50_us();
wait_50_us();
}
static inline void wait_50_ms(void)
{
/* FIXME: was _delay_ms(50), but not working */
uint8_t x;
for (x = 0; x < 25; ++x) wait_2_ms();
}
static inline void lcd_pulse_en(void)
{
/* assume EN low */
LCD_PORT_EN |= LCD_MASK_EN;
wait_50_us();
LCD_PORT_EN &= ~LCD_MASK_EN;
wait_2_ms();
}
static void lcd_write_db4(uint8_t x)
{
/* configured in 4 bits mode */
LCD_PORT_DB &= ~LCD_MASK_DB;
LCD_PORT_DB |= (x >> 4) << LCD_POS_DB;
lcd_pulse_en();
LCD_PORT_DB &= ~LCD_MASK_DB;
LCD_PORT_DB |= (x & 0xf) << LCD_POS_DB;
lcd_pulse_en();
}
static void lcd_write_db8(uint8_t x)
{
/* configured in 8 bits mode */
/* only hi nibble transmitted, (0:3) grounded */
LCD_PORT_DB &= ~LCD_MASK_DB;
LCD_PORT_DB |= (x >> 4) << LCD_POS_DB;
lcd_pulse_en();
}
/* exported interface */
void lcd_setup(void)
{
LCD_DIR_DB |= LCD_MASK_DB;
LCD_DIR_RS |= LCD_MASK_RS;
LCD_DIR_EN |= LCD_MASK_EN;
LCD_PORT_DB &= ~LCD_MASK_DB;
LCD_PORT_RS &= ~LCD_MASK_RS;
LCD_PORT_EN &= ~LCD_MASK_EN;
/* small delay for the lcd to boot */
wait_50_ms();
/* datasheet init sequence */
#define LCD_MODE_BLINK (1 << 0)
#define LCD_MODE_CURSOR (1 << 1)
#define LCD_MODE_DISPLAY (1 << 2)
lcd_write_db8(0x30);
wait_2_ms();
wait_2_ms();
wait_500_ns();
lcd_write_db8(0x30);
wait_2_ms();
lcd_write_db4(0x32);
wait_2_ms();
lcd_write_db4(0x28);
wait_2_ms();
lcd_write_db4((1 << 3) | LCD_MODE_DISPLAY);
wait_2_ms();
lcd_write_db4(0x01);
wait_2_ms();
lcd_write_db4(0x0f);
wait_2_ms();
}
void lcd_clear(void)
{
/* clear lcd */
lcd_write_db4(0x01);
wait_2_ms();
}
void lcd_home(void)
{
/* set cursor to home */
lcd_write_db4(0x02);
wait_2_ms();
}
void lcd_set_ddram(uint8_t addr)
{
lcd_write_db4((1 << 7) | addr);
wait_2_ms();
}
void lcd_goto_xy(uint8_t x, uint8_t y)
{
/* assume 0 <= x < 8 */
/* assume 0 <= y < 2 */
/* from datasheet: */
/* first line is 0x00 to 0x27 */
/* second line is 0x40 to 0x67 */
static const uint8_t row[] = { 0x00, 0x40 };
lcd_set_ddram(row[y] | x);
}
void lcd_write(const uint8_t* s, unsigned int n)
{
wait_50_ns();
LCD_PORT_RS |= LCD_MASK_RS;
for (; n; --n, ++s)
{
lcd_write_db4(*s);
wait_2_ms();
}
LCD_PORT_RS &= ~LCD_MASK_RS;
}
ADXL345 Driver
#define ADXL_SDA PIN_C4
#define ADXL_SCL PIN_C3
#define ADXL_CS PIN_C0
#use i2c(master, sda=ADXL_SDA, scl=ADXL_SCL)
void init_adxl345()
{
output_float(ADXL_SCL);
output_float(ADXL_SDA);
output_high(ADXL_CS);
}
BOOLEAN adxl345_ready()
{
int1 ack;
i2c_start(); // If the write command is acknowledged,
ack = i2c_write(0x3a); // then the device is ready.
i2c_stop();
return !ack;
}
void write_adxl345(BYTE address, BYTE data)
{
while(!adxl345_ready());
i2c_start();
i2c_write(0x3a);
i2c_write(address);
i2c_write(data);
i2c_stop();
}
BYTE read_adxl345(BYTE address)
{
BYTE data;
while(!adxl345_ready());
i2c_start();
i2c_write(0x3a);
i2c_write(address);
i2c_start();
i2c_write(0x3b);
data=i2c_read(0);
i2c_stop();
return(data);
}
int16 read_adxl345_axis(BYTE address)
{
BYTE msb,lsb;
while(!adxl345_ready());
i2c_start();
i2c_write(0x3a);
i2c_write(address);
i2c_start();
i2c_write(0x3b);
lsb=i2c_read(1);
msb=i2c_read(0);
i2c_stop();
return((msb<<8)|lsb);
}
Binary Coded Decimal (BCD) - ASCII Converter
char bcdToAscii( unsigned char bcdNibble )
{
char result;
if( bcdNibble < 10 )
{// valid BCD input. ( [0,9] is the valid range for BCD input. )
result = (char)( bcdNibble + 48 ); // +48 is applicable to [0,9] input range.
}// end if
else
{// invalid input
result = '0';
}// end else
return( result );
}// end bcdToAscii()
unsigned char asciiToBcd( char asciiByte )
{/* Converts an input ASCII character (expected within the [ '0' - '9' ] range) into its BCD counterpart. */
unsigned char result;
if(
asciiByte >= '0'
&& asciiByte <= '9'
)
{// range check passed.
result = (unsigned char)(asciiByte - 48); // -48 offset gives the decimal value of the ASCII character.
}
else
{// range check failed.
result = 0;
}// end else
return( result );
}// end asciiToBcd()
Embedded Linux Frequency Meter
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <asm/arch/gpio.h>
#include <asm/arch/dmtimer.h>
/*
The prescaler is enabled when TCLR bit 5 is set (PRE). The 2n division ratio value
(PTV) can be configured in the TCLR register.
Each internal interrupt source can be independently enabled/disabled in the interrupt
enable register TIER.
When the interrupt event has been issued, the associated interrupt status bit is set
in the timer status register (TISR). The pending interrupt event is reset when the
set status bit is overwritten by a 1.
The timer rate is defined by:
-Value of the prescaler fields (PRE and PTV of TCLR register)
-Value loaded into the timer load register (TLDR)
timer rate = (0xFFFF FFFF – TLDR + 1) x timer clock period x clock divider (PS)
PTV + 1)
PS = 2
*/
static unsigned long freq , ct, round;
extern struct omap_dm_timer * frequencimeter; // timer reserved to measure frequency
static irqreturn_t gpio4_freqmeter_irq_handler(int irq, void *arg);
static int __init freqmeter_init(void)
{
int r;
round = 0; freq = 0 ; ct = 0;
printk(KERN_DEBUG "Init driver Freqmeter.\n");
/* request gpios*/
/* GPIO - P20_1610_GPIO4 */
if ( omap_request_gpio(4) < 0 ) printk(KERN_ERR "Error init GPIO4 (freqmeter).\n");
/* entrada */
omap_set_gpio_direction(4,1); /* in */
r = request_irq(OMAP_GPIO_IRQ(4), gpio4_freqmeter_irq_handler, IRQF_TRIGGER_RISING, "freqmeter", gpio4_freqmeter_irq_handler);
if ( r < 0 ) {
printk(KERN_ERR "freqmeter: request_irq() failed.\n");
return r;
}
printk(KERN_DEBUG "freqmeter initialized.\n");
return 0;
}
static irqreturn_t gpio4_freqmeter_irq_handler(int irq, void *arg)
{
// dummy: no interrupt? freq = 0Hz
// only one int? freq = 0Hz
/** there is interference?: lread INT again
should be in same logic level */
if ( omap_get_gpio_datain(4) )
{
if(round > 0)
{
if(round == 50)
{
ct = omap_dm_timer_read_counter(frequencimeter);
omap_dm_timer_stop(frequencimeter);
ct /= 50;
freq = 1200000000/(ct +1);
printk("freq = %d\n",(freq/*-8*/));
round = 0xFFFFFFFF;
ct = 0;
freq = 0;
}
}
else // first read
{
freq = 0;
printk(KERN_DEBUG "Iniciou o freqmeter");
omap_dm_timer_write_counter(frequencimeter,0x0);
omap_dm_timer_start(frequencimeter);
}
round++;
}
return IRQ_HANDLED;
}
asmlinkage long sys_freq_read(void)
{
return freq;
}
static void __exit freqmeter_cleanup(void)
{
free_irq(OMAP_GPIO_IRQ(4), NULL);
omap_free_gpio(4);
}
module_init(freqmeter_init);
module_exit(freqmeter_cleanup);
MODULE_LICENSE("GPL");
Integer to ASCII
/***** integerToAscii.h *****/
#define BASE_OCTAL 8
#define BASE_DECIMAL 10
#define BASE_HEXADECIMAL 16
#define BUFFER_SIZE 32
char* integerToASCII(long int value, int base);
/***** integerToAscii.c *****/
char* integerToASCII(long int value, int base){
int aux;
static char buf[BUFFER_SIZE] = {0};
int isNeg=0;
if (value == 0) {
buf[0]='0';
return &buf[0];
}
if (value<0) {
isNeg = 1;
value *= -1;
}
for(aux=BUFFER_SIZE; value && aux ; --aux, value /= base){
buf[aux] = "0123456789abcdef"[value % base];
}
if (isNeg) {
buf[aux] = '-';
return &buf[aux];
}
return &buf[aux+1];
}
Incremental 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);
}
}
