Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <hidef.h> /* common defines and macros */
- #include "derivative.h" /* derivative-specific definitions */
- #include <mc9s12dp256.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define GEN_VERSION 0x11
- #define CAMERABUFFSIZ 100
- #define STOPCURRENTFRAME 0x0
- #define FBUF_CTRL 0x36
- #define WRITE_DATA 0x31
- #define READ_DATA 0x30
- #define GET_FBUF_LEN 0x34
- #define Size_640x480 0x00
- #define Size_320x240 0x11
- #define Size_160x120 0x22
- #define RESET 0x26
- #define CAMERADELAY 10
- #define READ_FBUF 0x32
- #define STOP_FRAME 0x36
- #define BAUD_RATE_9600_1 0xAE
- #define BAUD_RATE_9600_2 0xC8
- #define BAUD_RATE_CMD 0x24
- unsigned char* getVersion(void);
- int setImageSize(void);
- void sendCommand(unsigned char cmd, unsigned char args[], unsigned char argn);
- unsigned char readResponse(unsigned char numbytes);
- int runCommand(unsigned char, unsigned char *, unsigned char,unsigned char, int);
- int verifyResponse(unsigned char );
- int takePicture(void);
- int cameraFrameBuffCtrl(unsigned char );
- int reset(void);
- unsigned int frameLength(void);
- unsigned char getImageSize(void);
- unsigned char* readPicture(unsigned char );
- unsigned int setBaudRate(void);
- int setCompression(unsigned char);
- unsigned char getCompression(void);
- unsigned int stopFrame(void);
- void delay(int);
- void sciWrite(char);
- void sciWrites(char temp[]);
- int check(char*,int);
- unsigned char camerabuff[CAMERABUFFSIZ+1];
- int bufferLen=0;
- unsigned char serialNum =0;
- unsigned int frameptr=0;
- float testSize;
- void main(void) {
- unsigned int count;
- unsigned int frameSize;
- unsigned char temp[20];
- unsigned int bytesToRead;
- int i=0;.
- unsigned char *buf;
- unsigned char compression=0;
- DDRH=0x00; //PortH input
- DDRB=0xFF; //PORTB output
- PORTB = 0;
- SCI1BDH=0;
- SCI1BDL=156; //baud rate set.
- SCI1CR1=0X00;
- SCI1CR2=0X0C; //Transimit & Receiver enable
- SCI0BDH=0;
- SCI0BDL=156
- SCI0CR1=0X00;
- SCI0CR2=0X08;
- PORTB=0;
- while((PTH&0x01)!=1);
- count = reset();
- delay(4000);
- count = setBaudRate();
- SCI1BDH=0;
- SCI1BDL=156;
- SCI1CR1=0X00;
- SCI1CR2=0X0C;
- count=setImageSize();
- compression=0xFA;
- count=setCompression(compression);
- buf = getVersion();
- count=getImageSize();
- count=takePicture();
- count=frameLength();
- frameSize =camerabuff[7]*256+camerabuff[8];
- while(frameSize > 0 ) {
- int index,i;
- unsigned char *buffer;
- bytesToRead = frameSize<32?frameSize:32;
- buffer = readPicture(bytesToRead);
- sciWrites(buffer);
- frameSize -= bytesToRead;
- }
- count=stopFrame();
- return ;
- }
- int check(char *buffer,int bytesToRead){
- int i;
- for(i=0; i<100;++i) {
- if((buffer[i]==0xFF)&&(buffer[i+1]=0xDA)) {
- return i+1;
- }
- }
- return -1;
- }
- int reset(void) {
- unsigned char args[] = {0x0};
- return runCommand(RESET, args, 1, 5,0);
- }
- int takePicture(void) {
- frameptr = 0;
- return cameraFrameBuffCtrl(STOPCURRENTFRAME);
- }
- unsigned int frameLength(void) {
- unsigned int len=0;
- unsigned char args[] = {0x01, 0x00};
- if (!runCommand(GET_FBUF_LEN, args, sizeof(args), 9,0))
- return 0 ;
- len = camerabuff[5];
- len <<= 8;
- len |= camerabuff[6];
- len <<= 8;
- len |= camerabuff[7];
- len <<= 8;
- len |= camerabuff[8];
- return len;
- }
- int cameraFrameBuffCtrl(unsigned char command) {
- unsigned char args[2];
- args[0]=0x1;
- args[1]=command;
- return runCommand(FBUF_CTRL, args, sizeof(args), 5,0);
- }
- unsigned char* getVersion(void){
- unsigned char args[]={0x01};
- sendCommand( GEN_VERSION, args, 1 );
- if(!readResponse( 100 ))
- return NULL;
- camerabuff[bufferLen]=0;
- return camerabuff;
- }
- int setImageSize(void) {
- unsigned char args[]={ 0x05, 0x04, 0x01, 0x00, 0x19, Size_160x120};
- return runCommand( WRITE_DATA, args, sizeof(args), 5,0);
- }
- int setCompression(unsigned char c){
- unsigned char args[6];
- args[0]=0x5;
- args[1]=0x1;
- args[2]=0x1;
- args[3]=0x12;
- args[4]=0x04;
- args[5]=c;
- return runCommand(WRITE_DATA,args,sizeof(args),5,0);
- }
- unsigned char getCompression(void) {
- unsigned char args[] = {0x4, 0x1, 0x1, 0x12, 0x04};
- runCommand(READ_DATA, args, sizeof(args), 6,0);
- return camerabuff[5];
- }
- unsigned char getImageSize() {
- unsigned char args[] = {0x4, 0x4, 0x1, 0x00, 0x19};
- if (! runCommand(READ_DATA, args, sizeof(args), 6,0))
- return -1;
- return camerabuff[5];
- }
- unsigned char* readPicture(unsigned char n) {
- unsigned char args[13];
- args[0]=0x0C;
- args[1]=0x0;
- args[2]=0x0A;
- args[3]=0;
- args[4]=0;
- args[5]=frameptr>>8;
- args[6]=frameptr&0xFF;
- args[7]=0;
- args[8]=0;
- args[9]=0;
- args[10]=n;
- args[11]=CAMERADELAY>>8;
- args[12]=CAMERADELAY>>8;
- //args[11]=0;
- //args[12]=10;
- if (!runCommand(READ_FBUF, args, sizeof(args), 5, 0))
- return 0;
- if (readResponse(n+5) == 0)
- return 0;
- frameptr += n;
- return camerabuff;
- }
- unsigned int setBaudRate(void){
- unsigned char args[]={0x03,0x01,BAUD_RATE_9600_1,BAUD_RATE_9600_2};
- return runCommand(BAUD_RATE_CMD,args,sizeof(args),5,0 );
- }
- unsigned int stopFrame(void) {
- unsigned char args[]={0x01,0x03};
- return runCommand(STOP_FRAME,args,sizeof(args),5,0);
- }
- int runCommand(unsigned char cmd, unsigned char *args, unsigned char argn,
- unsigned char resplen, int flushflag){
- if(flushflag)
- readResponse(100);
- sendCommand(cmd,args,argn);
- if(readResponse(resplen) !=resplen )
- return 0;
- if( !verifyResponse(cmd))
- return 0;
- return 1;
- }
- void sendCommand(unsigned char cmd, unsigned char args[], unsigned char argn){
- int i ;
- SCI1DRL = 0x56;
- while((SCI1SR1&0X80)==0);
- SCI1DRL = serialNum;
- while((SCI1SR1&0X80)==0);
- SCI1DRL = cmd;
- while((SCI1SR1&0X80)==0);
- for(i=0; i< argn ;++i) {
- SCI1DRL = args[i];
- while((SCI1SR1&0X80)==0);
- }
- return;
- }
- unsigned char readResponse(unsigned char numbytes){
- unsigned char counter=0;
- bufferLen=0;
- while(bufferLen!=numbytes) {
- while((SCI1SR1 & 0x20 )==0);
- camerabuff[bufferLen++]=SCI1DRL;
- }
- return bufferLen;
- }
- int verifyResponse(unsigned char command) {
- if ((camerabuff[0] != 0x76) ||
- (camerabuff[1] != serialNum) ||
- (camerabuff[2] != command) ||
- (camerabuff[3] != 0x0))
- return 0;
- return 1;
- }
- void sciWrites(char temp[]){
- int i;
- for(i=0; temp[i]!='\0';++i)
- sciWrite(temp[i]);
- }
- void sciWrite(char temp){
- SCI0DRL=temp;
- while((SCI0SR1&0X80)==0);
- }
- void delay(int num) {
- volatile unsigned short i;
- while(num>0) {
- i= 2000;
- while(i>0){
- i=i-1;
- }
- num=num-1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement