วันศุกร์ที่ 18 ธันวาคม พ.ศ. 2552

iNEXUS Thailand 2009 Competition on 13th of December 2009


on 13th of December me,my brother ,and my friends went to robot competition at AIT Thailand and we attend this robot competition.So I have to thank so much everyone who concern the robot competition establishment and who help me to finish this job,thanks Dr.Phongsak and Dr.Rewat to give so many things to use in the robot, thanks my friend (POOP) to bring many parts in the robot to me, thanks Joke, P'Tank, On and Big to help my team, thanks younger EE at KMUTNB to help me,finally Thanks AIT to establish the robot competition.THANK YOU.

these robot are small size like a mobile robot.They were called "ANTZ" follow the host by AIT and IIT BOMBAY.These was developed at MEST LAB KMUTNB . They were developed by Mr. Sakmongkon.

Well for this blog,I'd like to inform how to build these robot.
The robots consist of 3 systems.There are Mechanic system,Control Circuit system and robot firmware system.I would like to explain about Mechanic system first.

  1. Mechanic
    • Body
    • Wheels or moving system
    • Griper

  2. Control Circuit
    • Sensor Boar
    • Motor Drive Board
    • Microcontroller Board (ARM7)

  3. Firmware (By Keil)
    • Tracking line
    • Positioning control
    • Communication control
Hi.Today I'm going to show my robot pictures.





วันศุกร์ที่ 20 พฤศจิกายน พ.ศ. 2552

FAT32 By CCS is GOOD JOB



To day I want to show testing code library FAT32 with PIC18F(8722) by CCS.
  1. why did I decide to use CCS ?
    It was easy to answer as CCS is easy to use for well-know application on PIC.

  2. Provides development tools
    • Software tools
      • CCS
      • Hyperterminal
    • Hardware tools
      • PIC18F board with SD/MMC socket
      • PICKit or somethings
  3. Experiment
Code :
/////////////////////////////////////////////////////////////////////////
//// fat_ex_shell.c ////
//// ////
//// This is a neat little shell utility that resembles a DOS or ////
//// UNIX type shell in order to manipulate files on the FAT file ////
//// system. This is mostly for demonstration purposes on how to use ////
//// some of the basic functionality of the FAT library. ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 2007 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////

//These settings are for the CCS PICEEC development kit which contains
//an MMC/SD connector.
#include <18f8722.h>
#device PASS_STRINGS = IN_RAM
#fuses NOWDT, HS, NOPROTECT
#use delay(clock=10M)

#use rs232(baud=9600, UART1, errors)

#include // for atoi32

//media library, a compatable media library is required for FAT.
#use fast_io(c)
#define MMCSD_PIN_SCL PIN_C3 //o
#define MMCSD_PIN_SDI PIN_C4 //i
#define MMCSD_PIN_SDO PIN_C5 //o
#define MMCSD_PIN_SELECT PIN_C2 //o
#include

//FAT library.
#include

//////////////////////
/// ///
/// Useful Defines ///
/// ///
//////////////////////

#define COMMAND_SIZE 10
#define NUM_COMMANDS 11

////////////////////////
/// ///
/// Global Variables ///
/// ///
////////////////////////

char g_CWD[200] = "/"; //current working directory

char commands[NUM_COMMANDS][COMMAND_SIZE]=
{
"del", //option1=filename. delete file.
"make", //option1=filename. create an empty file, give error if file already exists
"append", //option1=filename, option2=string. append string to end of file
"cd", //option1=new cwd. change working directory. / is root.
"dir", //show files in directory
"cat", //option1=filename. display full contents in ascii
"tail", //option1=filename. display the last 20 lines of file.
"mkdir", //option1=dir. create directory. see 'cd' for rules on dir
"rmdir", //option1=dir. remove directory. see 'cd' for rules on dir.
"format", // option1=media size in bytes. formats the media.
"help" // help!
};

////////////////////////////////
/// ///
/// Function Implementations ///
/// ///
////////////////////////////////

/*
Summary: Finds a command from the global list of commands.
Param: A pointer to the command string.
Returns: The command number if the command is found in the command list.
0xFF if the command isn't found
*/
int FindCommand(char *cmd)
{
char buf[COMMAND_SIZE];
int i;

for (i=0; i ", g_CWD);
}

/*
Summary: Deletes a file.
Param: The full path of the file to delete.
Returns: None.
*/
void DeleteFile(char *fileName)
{
printf("\r\nDeleting '%s': ", fileName);
if(rm_file(fileName) != GOODEC)
{
printf("Error deleting file");
return;
}
printf("OK");
}

/*
Summary: Creates a file.
Param: The full path of the file to create.
Returns: None.
Example Usage: \> make "Log.txt"
*/
void MakeFile(char *fileName)
{
printf("\r\nMaking file '%s': ", fileName);
if(mk_file(fileName) != GOODEC)
{
printf("Error creating file");
return;
}
printf("OK");
}

/*
Summary: Append a string to a file.
Param: The full path of the file to append to.
Param: A pointer to a string to append to the file.
Returns: None.
Example Usage: \> append "Log.txt" "This will be appended to the end of Log.txt"
Note: A "\r\n" will be appended after the appendString.
*/
void AppendFile(char *fileName, char *appendString)
{
FILE stream;
printf("\r\nAppending '%s' to '%s': ", appendString, fileName);
if(fatopen(fileName, "a", &stream) != GOODEC)
{
printf("Error opening file");
return;
}

fatputs(appendString, &stream);
fatputs("\r\n", &stream);

if(fatclose(&stream) != GOODEC)
{
printf("Error closing file");
return;
}
printf("OK");
}

/*
Summary: Change the working directory.
Param: The new working directory to switch to.
Returns: None.
Example Usage: \> cd ftp/ -> /ftp/
\ftp\> cd files/ -> /ftp/files/
\ftp\files> cd.. -> /ftp/
\ftp\> cd .. -> /
\> cd /ftp/files/ -> /ftp/files/

Note: Changing the directory to .. will go up a directory.
*/
void ChangeDirectory(char *newCWD)
{
FILE stream;

//append a / to the end of the filename if it doesn't exist
//making an assumption here that newCWD can hold 1 more character
if (newCWD[strlen(newCWD)-1] != '/')
strcat(newCWD, "/");

if((strstr(newCWD, "../") != 0) && (strcmp(g_CWD, "/") != 0))
{
g_CWD[strlen(g_CWD) - 1] = '\0';

g_CWD[strrchr(g_CWD, '/') - g_CWD + 1] = '\0';
}
else
{
if(fatopen(newCWD, "r", &stream) != GOODEC)
{
printf("\r\nError changing directory");
return;
}
strcpy(g_CWD, newCWD);
}
}

/*
Summary: Display the contents of the working directory.
Param: The full path of the directory contents to display.
Returns: None.
Example Usage: /> dir
*/
void DisplayDirectory(char *dir)
{
disp_folder_contents(dir);
}

/*
Summary: Create a directory.
Param: The full path of the directory to create.
Returns: None.
Example Usage: /> mkdir "Backlog"
*/
void MakeDirectory(char *dir)
{
//append a / to the end of the filename if it doesn't exist
//making an assumption here that newCWD can hold 1 more character
if (dir[strlen(dir)-1] != '/')
strcat(dir, "/");

printf("\r\nMaking directory '%s': ", dir);

if(mk_dir(dir) != GOODEC)
{
printf("Error creating directory");
return;
}
printf("OK");
}

/*
Summary: Remove a directory.
Param: The full path of the directory to remove.
Returns: None.
Example Usage: /> rmdir "Backlog"
Note: The directory needs to be empty in order for this command to work.
*/
void RemoveDirectory(char *dir)
{
printf("\r\nRemoving directory '%s': ", dir);

//append a / to the end of the filename if it doesn't exist
//making an assumption here that newCWD can hold 1 more character
if (dir[strlen(dir)-1] != '/')
strcat(dir, "/");

if(rm_dir(dir) != GOODEC)
{
printf("Error removing directory");
return;
}
printf("OK");
}

#define CAT_FROM_START FALSE
#define CAT_FROM_END TRUE
/*
Summary: Prints either all of or the last 80 characters in a file.
Param: The full path of the file to print off.
Param: If true, this function will print off the last 80 characters in the file.
If false, this funciton will print off the entire file.
Returns: None.
Example Usage: /> cat "Logs.txt" (this will display the entire file)
Example Usage: /> tail "Logs.txt" (this will display the last 80 characters in the file)
*/
void PrintFile(char *fileName, int1 startFromEnd)
{
FILE stream;

if(fatopen(fileName, "r", &stream) != GOODEC)
{
printf("\r\nError opening file");
return;
}

printf("\r\n");

if(startFromEnd)
fatseek(&stream, 80, SEEK_END);

fatprintf(&stream);
fatclose(&stream);
}

/*
Summary: Formats the media to a specified size.
Param: The size of the media, in kB, in string form.
Returns: None.
Example Usage: /> format 524288 (format a 512MB card)
*/
void FormatMedia(char *mediaSize)
{
int32 size;

size = atoi32(mediaSize);

printf("\r\nFormatting media (size=%LU): ", size);

if(format(size) != GOODEC)
{
printf("Error formatting media");
return;
}
printf("OK");
}

/*
Summary: Shows a help prompt.
Param: None.
Returns: None.
Example Usage: /> help
*/
void ShowHelp()
{
printf("\r\nFAT Shell Help");
printf("\r\n del filename --- Deletes the file");
printf("\r\n make filename --- Creates an empty file");
printf("\r\n append filename string --- Appends string to the end of the file");
printf("\r\n cd dir --- Change the working directory");
printf("\r\n dir --- Shows the contents of the directory");
printf("\r\n cat filename --- Displays content of file");
printf("\r\n tail filename --- Displays the last 80 characters of file");
printf("\r\n mkdir dir --- Create a directory");
printf("\r\n rmdir dir --- Deletes the directory");
printf("\r\n format size --- Format card. (Example: 'format 5524288' formats a 512MB card)");
printf("\r\n help\tYou are here");
printf("\r\n\n Put a parameter in quotes if it has spaces");
}

char * GetCMD(char *in)
{
char tokens[]=" \r\n";
return(strtok(in,tokens));
}

char * GetOption(char *in)
{
char tokensSpace[]=" \r\n";
char tokensQuote[]="\"\r\n";

//trim leading spaces
while (*in==' ')
in++;

//if first char is a quote, then end token on a quote. ELSE end token on a space
if (*in == '\"')
return(strtok(in,tokensQuote));
else
return(strtok(in,tokensSpace));
}

void main(void)
{
char buffer[255];
char opt_buffer[255];
char *cmd, *option1, *option2;

int i; // pointer to the buffer

// initialize the FAT
// keep in mind that this will automagically initialize the media
i = fat_init();
if (i)
printf("\r\n\nERROR INITIALIZING FAT\r\n\n");

printf("\r\n\nStart FAT = %d\r\n\n",i);
//delay_ms(2000);
//AppendFile("/m.txt","started\r\n asdfjkl;sf\r\n\tkl;'sdkflasd'fkl");
// main loop
while(TRUE)
{
i = 0;

DisplayPrompt();

do
{
buffer[i] = getch();

// check for a backspace
if(buffer[i] != 8)
{
printf("%c", buffer[i]);
i++;
}
else if(i > 0)
{
// delete the last character
i--;
putc(8);
putc(' ');
putc(8);
}
buffer[i] = '\0';
} while(buffer[i - 1] != '\r');

// parse the command and options
cmd = GetCMD(buffer);
option1 = GetOption(cmd + strlen(cmd) + 1);
option2 = GetOption(option1 + strlen(option1) + 1);

//if option1 starts with a '/', that means the file in the option includes
//the full path to the file. if the file doesn't start with a '/', the
//current working directory must be added.
if (option1 && (option1[0]=='/'))
{
//option1 is a full path
strcpy(opt_buffer, option1);
}
else if (option1)
{
// tack on the current working directory to option1
strcpy(opt_buffer, g_CWD);
strcat(opt_buffer, option1);
}

if (cmd)
{
switch(FindCommand(cmd))
{
case 0: //del
DeleteFile(opt_buffer);
break;

case 1: //make
MakeFile(opt_buffer);
break;

case 2: //append
AppendFile(opt_buffer, option2);
//printf("=%s =%s",opt_buffer,option2);
break;

case 3: //change directory
ChangeDirectory(opt_buffer);
break;

case 4: //show directory contents
DisplayDirectory(g_CWD);
break;

case 5: //cat, display file
PrintFile(opt_buffer, CAT_FROM_START);
break;

case 6: //tail, display last 80 charachters
PrintFile(opt_buffer, CAT_FROM_END);
break;

case 7: //mkdir, make a directory
MakeDirectory(opt_buffer);
break;

case 8: //rmdir, make a directory
RemoveDirectory(opt_buffer);
break;

case 9: //format, format the card
FormatMedia(option1);
break;

case 10: //help, display help
ShowHelp();
break;

default:
printf("\r\nUnkown Command '%s'", cmd);
break;
}
}
}
}


Thank you
CCS
Microchip
Mr.Sakmongkon Chumkamon
let me know your Acknowledge.

วันอาทิตย์ที่ 18 ตุลาคม พ.ศ. 2552

Measuring accelerometer on LPC2148


Hi ,
today I'm grad to show my little project about how to program to read analog signal from accelerometer by LPC2148(ARM7) . so let see.

Development tools
  • Keil Compliler
  • ADXL3XX from paralax
  • LPC2148 board by SiSy-Works
  • laptop
  • USB to Serial converter


Source CODE
//This code is simple.
//Sorry if it is not good code.

#include "LPC214X.H" //change "" to <>
#include "stdio.h" //change "" to <>
#define _PCLK 30000000 // Define PCLK for configuration baudrate
#define FWD() FIO1SET = 0x00C50000;\
FIO1CLR = 0x003A0000
#define BCK() FIO1SET = 0x00CA0000;\
FIO1CLR = 0x00350000
#define LFT() FIO1SET = 0x00C90000;\
FIO1CLR = 0x003E0000
#define RGT() FIO1SET = 0x00C60000;\
FIO1CLR = 0x003B0000
#define Brake() FIO1SET = 0x00CF0000

unsigned char index=0, temp_index=0, blue_con=0;
unsigned char Buffer[64];
unsigned char rec_comp=0;

void init(void)
{
PLL0CFG=0x24; // MSEL = 4,PSEL = 2
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;

PLL0CON=0x1;
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;

while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked

PLL0CON=0x3; // Connect the PLL as the clock source
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;

MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch (4 cclks in this case)
MAMTIM=0x4;

VPBDIV=0x02; // PCLK at 30 MHz

//----set up pinsel-----------//
PINSEL0 = 0x00000000; // Clear PINSEL0
PINSEL0 |= 0x00003F00; // ADC Pins P0.04-P0.06 (AD 0.6/0.7/1.0)
//PINSEL1
PINSEL1 = 0x00000000; // Clear PINSEL1
PINSEL1 |= 0x00001800; // ADC Pins P0.21-P0.22 (AD 1.6/1.7)
PINSEL1 |= 0x15000000; // ADC Pins P0.28-P0.30 (AD 0.1/0.2/0.3)
IODIR0 &= 0x8F9FFF8F; // ADC Pins as Input Pins
}
void delay_ms(long ms) // delay 1 ms per count @ CCLK 60 MHz
{
long i,j;
for (i = 0; i < j =" 0;" u0dl =" _PCLK/(16*_baudrate);" u0lcr =" 0x00000083;" u0dll =" u0dl" u0dlm =" (u0dl">>8); // U0DL for high byte
U0LCR = 0x00000003; // DLAB =0
}
void uart1_init(unsigned int _baudrate)
{
unsigned short u1dl;
u1dl = _PCLK/(16*_baudrate); // Calculate for U1DL value
PINSEL0 |= 0x00050000; // Enable rx,tx
U1LCR = 0x00000083; // 8 bit data,1 stop bit,no parity bit
U1DLL = u1dl & 0xFF; // U1DL for low byte
U1DLM = (u1dl>>8); // U1DL for high byte
U1LCR = 0x00000003; // DLAB =0
}
void uart0_putc(char c)
{
while(!(U0LSR & 0x20)); // Wait until UART0 ready to send character
U0THR = c; // Send character
}
void uart0_puts(char *p)
{
while(*p) // Point to character
{
uart0_putc(*p++); // Send character then point to next character
}
}
short int isr_u1=0;
void isr_uart1(void) __irq
{
char msg;
char temp;
isr_u1=1;
if(((msg = U1IIR) & 0x01) == 0) // Check status flag communication
{
switch (msg & 0x0E) // Filter message
{
case 0x04: while(!(U1LSR & 0x20)); // Receive Data Available
temp = U1RBR;
//uart0_putc(temp);
if (temp == '\r')
//if (temp == '1')
{
rec_comp = 1;
Buffer[index] = temp;
temp_index = index;
index = 0;
}
else
{
rec_comp = 0;
Buffer[index] = temp;
index++;
}
break;

case 0x02: break; // THRE Interrupt

default: break;
}
}
VICVectAddr = 0; // Acknowledge Interrupt
}
void init_ADC(void){

// ADCR: - AD Control Register
// *****
// Bit 21 PDN = 1 : AD Converter is Operational
// Bit 17-19 CLKS = 000 : Resolution = 10 Bits / 11 Clocks
// Bit 16 BURST = 0 : Automated AD Channel Conversion on -> Software Controled Mod off
// Bit 8-15 CLKDIV = 00000110: AD-CLK = PCLK/(CLKDIV+1) 4.5MHz
// Bit 7- 0 SEL = 00000010: ADC Chanel default = ADC_01


// Initial AD0CR (ADCR=0x01210602)
AD0CR &= 0x00000000; // Clear All Bit Control
AD0CR |= 0x000000CE; // SEL = ADC_0.1-0.3 / ADC_0.6-0.7
AD0CR |= 0x00000600; // CLKDIV=6
AD0CR |= 0x00010000; // BURST = 1 = Conversion Continue
AD0CR &= 0xFFF1FFFF; // CLKS = 000 = 10Bit/ 11 Clock Cycles
AD0CR |= 0x00200000; // PDN = 1 = Active ADC Module
AD0CR &= 0xF7FFFFFF; // EDGE = 0 = Conversion on Falling Edge
AD0CR |= 0x00000000; // START = 000 = Start Conversion Now

// Initial AD1CR (ADCR=0x01210602)
AD1CR &= 0x00000000; // Clear All Bit Control
AD1CR |= 0x000000C1; // SEL = ADC_1.0 / ADC_1.6-1.7
AD1CR |= 0x00000600; // CLKDIV=6
AD1CR |= 0x00010000; // BURST = 1 = Conversion Continue
AD1CR &= 0xFFF1FFFF; // CLKS = 000 = 10Bit/ 11 Clock Cycles
AD1CR |= 0x00200000; // PDN = 1 = Active ADC Module
AD1CR &= 0xF7FFFFFF; // EDGE = 0 = Conversion on Falling Edge
AD1CR |= 0x00000000; // START = 000 = Start Conversion Now


// Initial ADINTEN (ADINTEN=0)
AD0INTEN = 0; // Disable ADC Interrupt
AD1INTEN = 0;
}
//**********************************************/;
//* Read functions for the ADC Channel 0-7 */;
//**********************************************/;
unsigned int Analog_0(void){

unsigned int value;

//Start Read ADC 0.1 (Pin 0.28 - Sensor 0)
do // Loop Read ADC0
{
value = AD0DR1; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}

unsigned int Analog_1(void){

unsigned int value;

//Start Read ADC 0.2 (Pin 0.29 - Sensor 1)
do // Loop Read ADC0
{
value = AD0DR2; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}

unsigned int Analog_2(void){

unsigned int value;

// Start Read ADC 0.3 (Pin 0.30 - Sensor 2)
do // Loop Read ADC0
{
value = AD0DR3; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}

unsigned int Analog_3(void){

unsigned int value;

//Start Read ADC 0.6 (Pin 0.04 - Sensor 3)
do // Loop Read ADC0
{
value = AD0DR6; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}

unsigned int Analog_4(void){

unsigned int value;

//Start Read ADC 0.7 (Pin 0.05 - Sensor 4)
do // Loop Read ADC0
{
value = AD0DR7; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}

unsigned int Analog_5(void){

unsigned int value;

//Start Read ADC 1.0 (Pin 0.06 - Sensor 5)
do // Loop Read ADC0
{
value = AD1DR0; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}

unsigned int Analog_6(void){

unsigned int value;

//Start Read ADC 1.6 (Pin 0.21 - Sensor6)
do // Loop Read ADC0
{
value = AD1DR6; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer

return value;
}
unsigned int Analog_7(void){

unsigned int value;

//Start Read ADC 1.7 (Pin 0.22 - Sensor 7)
do // Loop Read ADC0
{
value = AD1DR7; // Read A/D Data Register
}
while ((value & 0x80000000) == 0); // Wait for ADC Conversion Complete
value = (value >> 6) & 0x03FF; // Shift ADC Result to Integer
return value;
}
int main(void)
{ int i=0;
unsigned int x,y,z;
char buff[32];
init();

uart0_init(9600);
uart1_init(9600);

U1IER = 3; // Enable rx/tx interrupt
PINSEL0 |= (1<<18); vicvectaddr0 =" (unsigned)isr_uart1;" vicvectcntl0 =" 0x20" vicintenable =" 1" i =" 5;" x="0;y="0;z="0;" i="0;i<10;i++)" x =" x/10;" y =" y/10;" z =" z/10;" x =" %5d" y =" %5d" z=" %5d">