Vehicle Model Using PWM and ADC

J

Thread Starter

John Adams

I am about writing a vehicle model code in C. I have to incorporate the concepts of PWM and ADC. I am using a Samsung ARM-11 board,and Adafruit LSM303 sensor and a servo motor. I am requesting assistance to start writing my code.

The following is my c code to control the LSM303 module, it is controlled by the S3C6410 board:<pre>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <sys/stat.h>
#include "lsm303_sensor.h"



#define CTRL_REG1_A (0x20)
#define CTRL_REG2_A (0x21)
#define CTRL_REG3_A (0x22)
#define CTRL_REG4_A (0x23)
#define CTRL_REG5_A (0x24)
#define CTRL_REG6_A (0x25)
#define OUT_X_L_A (0x28)
#define OUT_X_H_A (0x29)
#define OUT_Y_L_A (0x2A)
#define OUT_Y_H_A (0x2B)
#define OUT_Z_L_A (0x2C)
#define OUT_Z_H_A (0x2D)

#define CRA_REG_M (0x00)
#define CRB_REG_M (0x01)
#define MR_REG_M (0x02)
#define OUT_X_H_M (0x03)
#define OUT_X_L_M (0x04)
#define OUT_Z_H_M (0x05)
#define OUT_Z_L_M (0x06)
#define OUT_Y_H_M (0x07)
#define OUT_Y_L_M (0x08)
#define SR_REG_M (0x09)
#define TEMP_OUT_H_M (0x31)
#define TEMP_OUT_L_M (0x32)

#define LSM_303_ACCEL_ADDR (0x19)
#define LSM_303_MAGNET_ADDR (0x1E)

#define usage_if(a) do { do_usage_if( a , __LINE__); } while(0);


static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
int size, union i2c_smbus_data *data)
{
struct i2c_smbus_ioctl_data args;

args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(file,I2C_SMBUS,&args);
}


static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
{
return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
}

static inline __s32 i2c_smbus_read_byte(int file)
{
union i2c_smbus_data data;
if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
return -1;
else
return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
{
return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
I2C_SMBUS_BYTE,NULL);
}

static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
{
union i2c_smbus_data data;
if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
I2C_SMBUS_BYTE_DATA,&data))
return -1;
else
return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
__u8 value)
{
union i2c_smbus_data data;
data.byte = value;
return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_BYTE_DATA, &data);
}

static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
{
union i2c_smbus_data data;
if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
I2C_SMBUS_WORD_DATA,&data))
return -1;
else
return 0x0FFFF & data.word;
}

static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
__u16 value)
{
union i2c_smbus_data data;
data.word = value;
return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_WORD_DATA, &data);
}

static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
{
union i2c_smbus_data data;
data.word = value;
if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_PROC_CALL,&data))
return -1;
else
return 0x0FFFF & data.word;
}


/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
__u8 *values)
{
union i2c_smbus_data data;
int i;
if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
I2C_SMBUS_BLOCK_DATA,&data))
return -1;
else {
for (i = 1; i <= data.block[0]; i++)
values[i-1] = data.block;
return data.block[0];
}
}

static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
__u8 length, __u8 *values)
{
union i2c_smbus_data data;
int i;
if (length > 32)
length = 32;
for (i = 1; i <= length; i++)
data.block = values[i-1];
data.block[0] = length;
return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_BLOCK_DATA, &data);
}


#define CHECK_I2C_FUNC( var, label ) \
do { if(0 == (var & label)) { \
fprintf(stderr, "\nError: " \
#label " function is required. Program halted.\n\n"); \
exit(1); } \
} while(0);

int lsm303_open(char *dev_fqn, int addr, struct lsm303* e)
{
int funcs, fd, r;
e->fd = e->addr = 0;
e->dev = 0;

fd = open(dev_fqn, O_RDWR);
if(fd <= 0)
{
fprintf(stderr, "Error lsm303_open: %s\n", strerror(errno));
return -1;
}

// get funcs list
if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0))
{
fprintf(stderr, "Error ioctl I2C_FUNCS: %s\n", strerror(errno));
return -1;
}


// check for req funcs
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA );

// set working device
if( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0)
{
fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
return -1;
}
e->fd = fd;
e->addr = addr;
e->dev = dev_fqn;

return 0;
}

int lsm303_close(struct lsm303 *e)
{
close(e->fd);
e->fd = -1;
e->dev = 0;

return 0;
}

#if 0
int eeprom_24c32_write_byte(struct lsm303 *e, __u16 mem_addr, __u8 data)
{
__u8 buf[3] = { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
return i2c_write_3b(e, buf);
}


int eeprom_24c32_read_current_byte(struct lsm303* e)
{
ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
return i2c_smbus_read_byte(e->fd);
}

int eeprom_24c32_read_byte(struct lsm303* e, __u16 mem_addr)
{
int r;
ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
__u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
r = i2c_write_2b(e, buf);
if (r < 0)
return r;
r = i2c_smbus_read_byte(e->fd);
return r;
}
#endif

#if 0
int lsm303_read_current_byte(struct lsm303* e)
{
ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
return i2c_smbus_read_byte(e->fd);
}

int lsm303_read_byte(struct lsm303* e, __u16 mem_addr)
{
int r;
ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
if(e->type == EEPROM_TYPE_8BIT_ADDR)
{
__u8 buf = mem_addr & 0x0ff;
r = i2c_write_1b(e, buf);
} else if(e->type == EEPROM_TYPE_16BIT_ADDR) {
__u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
r = i2c_write_2b(e, buf);
} else {
fprintf(stderr, "ERR: unknown eeprom type\n");
return -1;
}
if (r < 0)
return r;
r = i2c_smbus_read_byte(e->fd);
return r;
}

int lsm303_write_byte(struct lsm303 *e, __u16 mem_addr, __u8 data)
{
if(e->type == EEPROM_TYPE_8BIT_ADDR) {
__u8 buf[2] = { mem_addr & 0x00ff, data };
return i2c_write_2b(e, buf);
} else if(e->type == EEPROM_TYPE_16BIT_ADDR) {
__u8 buf[3] =
{ (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
return i2c_write_3b(e, buf);
}
fprintf(stderr, "ERR: unknown eeprom type\n");
return -1;
}

#endif

void do_usage_if(int b, int line)
{
const static char *lsm303_usage =
"I2C-LSM303 Program, ONLY FOR TEST!\n";

if(!b)
return;

fprintf(stderr, "%s\n[line %d]\n", lsm303_usage, line);
exit(1);
}


#define die_if(a, msg) do { do_die_if( a , msg, __LINE__); } while(0);

void do_die_if(int b, char* msg, int line)
{
if(!b)
return;
fprintf(stderr, "Error at line %d: %s\n", line, msg);
fprintf(stderr, " sysmsg: %s\n", strerror(errno));
exit(1);
}

#if 0
static int read_from_lsm303(struct lsm303 *e, int addr, int size)
{
int ch, i;
for(i = 0; i < size; ++i, ++addr)
{
die_if((ch = lsm303_read_byte(e, addr)) < 0, "read error");
if( (i % 16) == 0 )
{
printf("\n %.4x| ", addr);
}
else if( (i % 8) == 0 )
{
printf(" ");
}

printf("%.2x ", ch);
fflush(stdout);
}

fprintf(stderr, "\n\n");

return 0;
}


static int write_to_lsm303(struct lsm303 *e, int addr)
{
int i;
for(i=0, addr=0; i<256; i++, addr++)
{
if( (i % 16) == 0 )
printf("\n %.4x| ", addr);
else if( (i % 8) == 0 )
printf(" ");
printf("%.2x ", i);
fflush(stdout);
die_if(lsm303_write_byte(e, addr, i), "write error");
}
fprintf(stderr, "\n\n");
return 0;
}

#endif
/**********************Access Accelerometer**************/
int accread()
{
struct lsm303 e;
int recvData[8]={0}, ret=0, i=0;

ret = lsm303_open("/dev/i2c/0", LSM_303_ACCEL_ADDR, &e);
if(ret < 0)
{
printf("Unable to open Accelerometer device file \n");
return -1;
}

// Enable Accelerometer
ret = i2c_smbus_write_byte_data(e.fd , CTRL_REG1_A, 0x27);
if(ret<0)
{
printf("Error writing data\n");
return -1;
}

ret = i2c_smbus_write_byte_data(e.fd, CTRL_REG4_A, 0x28);
if(ret<0)
{
printf("Error writing data\n");
return -1;
}

// Read data from Acceleromemter
recvData[0] = i2c_smbus_read_byte_data(e.fd, OUT_X_L_A);
recvData[1] = i2c_smbus_read_byte_data(e.fd, OUT_X_H_A);
recvData[2] = i2c_smbus_read_byte_data(e.fd, OUT_Y_L_A);
recvData[3] = i2c_smbus_read_byte_data(e.fd, OUT_Y_H_A);
recvData[4] = i2c_smbus_read_byte_data(e.fd, OUT_Z_L_A);
recvData[5] = i2c_smbus_read_byte_data(e.fd, OUT_Z_H_A);

for (i = 0 ; i < 6 ; i++)
{
printf("Received Accelerometer data is %d\n",recvData);
}

lsm303_close(&e);
return (0);
}

int magread()
{
struct lsm303 e;
int recvData[8]={0}, ret=0, i=0;
/***********************Access Magnetometer***************************************/

ret = lsm303_open("/dev/i2c/0", LSM_303_MAGNET_ADDR, &e);
if(ret < 0)
{
printf("Unable to open Magnetometer device file \n");
return;
}


ret = i2c_smbus_write_byte_data(e.fd, CRA_REG_M, 0x94);
if(ret<0)
{
printf("Error writing data %d\n",__LINE__);
return;
}

ret = i2c_smbus_write_byte_data(e.fd, MR_REG_M, 0x00);
if(ret<0)
{
printf("Error writing data %d\n",__LINE__);
return;
}

// Read data from Magnetometer
recvData[0] = i2c_smbus_read_byte_data(e.fd, OUT_X_H_M);
recvData[1] = i2c_smbus_read_byte_data(e.fd, OUT_X_L_M);
recvData[2] = i2c_smbus_read_byte_data(e.fd, OUT_Y_H_M);
recvData[3] = i2c_smbus_read_byte_data(e.fd, OUT_Y_L_M);
recvData[4] = i2c_smbus_read_byte_data(e.fd, OUT_Z_H_M);
recvData[5] = i2c_smbus_read_byte_data(e.fd, OUT_Z_L_M);
recvData[6] = i2c_smbus_read_byte_data(e.fd, TEMP_OUT_H_M);
recvData[7] = i2c_smbus_read_byte_data(e.fd, TEMP_OUT_L_M);


for (i = 0 ; i < 8 ; i++)
{
printf("Received Magnetometer data is %d\n",recvData);
}

lsm303_close(&e);

return 0;
}



This is my pwm code: -

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0

#define ESC_KEY 0x1b

int getch(void)
{
struct termios oldt,newt;
int ch;

if (!isatty(STDIN_FILENO)) {
fprintf(stderr, "this problem should be run at a terminal\n");
exit(1);
}
// save terminal setting
if(tcgetattr(STDIN_FILENO, &oldt) < 0) {
perror("save the terminal setting");
exit(1);
}

// set terminal as need
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
if(tcsetattr(STDIN_FILENO,TCSANOW, &newt) < 0) {
perror("set terminal");
exit(1);
}

ch = getchar();

// restore termial setting
if(tcsetattr(STDIN_FILENO,TCSANOW,&oldt) < 0) {
perror("restore the termial setting");
exit(1);
}
return ch;
}

static int fd = -1;
void close_buzzer(void);
void open_buzzer(void)
{
fd = open("/dev/pwm_sanman", 0);
if (fd < 0) {
perror("open pwm_buzzer device");
exit(1);
}

// any function exit call will stop the buzzer
atexit(close_buzzer);
}

void close_buzzer(void)
{
if (fd >= 0) {
ioctl(fd, PWM_IOCTL_STOP);
if (ioctl(fd, 2) < 0) {
perror("ioctl 2:");
}
close(fd);
fd = -1;
}
}

void set_buzzer_freq(int freq)
{
// this IOCTL command is the key to set frequency
int ret = ioctl(fd, PWM_IOCTL_SET_FREQ, freq);
if(ret < 0) {
perror("set the frequency of the buzzer");
exit(1);
}
}
void stop_buzzer(void)
{
int ret = ioctl(fd, PWM_IOCTL_STOP);
if(ret < 0) {
perror("stop the buzzer");
exit(1);
}
if (ioctl(fd, 2) < 0) {
perror("ioctl 2:");
}
}
#if 0
int main(int argc, char **argv)
{
int freq = 1000 ;

open_buzzer();

printf( "\nBUZZER TEST ( PWM Control )\n" );
printf( "Press +/- to increase/reduce the frequency of the BUZZER\n" ) ;
printf( "Press 'ESC' key to Exit this program\n\n" );


while( 1 )
{
int key;

set_buzzer_freq(freq);
printf( "\tFreq = %d\n", freq );

key = getch();

switch(key) {
case '+':
if( freq < 20000 )
freq += 10;
break;

case '-':
if( freq > 11 )
freq -= 10 ;
break;

case ESC_KEY:
case EOF:
stop_buzzer();
exit(0);

default:
break;
}
}
}
#endif</pre>
Please get back to me regarding the incorporation of these two code modules and start writing my ADC code.

Thanks
Email: [email protected]
 
Top