Thông báo

Collapse
No announcement yet.

tạo file.jpg trêm AVR và ghi vào thẻ nhớ Sd.

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • tạo file.jpg trêm AVR và ghi vào thẻ nhớ Sd.

    chào các bác. hiện giờ em đang ngâm cứu cái này mà chưa được. các bác có thể giúp em được không. em có chuỗi mã ảnh rùi mà khi ghi vô thẻ nhớ thì không có tên mà nó chỉ ẩn thui. kiểm tra dung luong tren thẻ nhớ thì vẫn có.
    OV0706 + avr + sd card
    đây là code giao tiep cua em:

    #include <mega32.h>
    #include <delay.h>
    #include <stdio.h>
    unsigned char reset[4]={0x56,0x00,0x26,0x00};

    unsigned char take[5]={0x56,0x00,0x36,0x01,0x00};
    unsigned char Rtake[5]={0x76,0x00,0x36,0x00,0x00};
    unsigned char read_size[5]={0x56,0x00,0x34,0x01,0x00};
    unsigned char read_JPEG[12]={0x56,0x00,0x32,0x0C,0x00,0x0A,0x00,0x00,0x00,0x0 0,0x00,0x00};
    unsigned char stop[5]={0x56,0x00,0x36,0x01,0x03};
    unsigned char data[20],buff[512];
    int j,n,k;
    //mbed.org/cookbook/camera_LS_201
    // Declare your global variables here

    //// MOI xxx



    //mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

    #define SPI_PORT PORTB
    #define SPI_DDR DDRB
    #define SCK_PIN PORTB.7
    #define MISO_PIN PORTB.6
    #define MOSI_PIN PORTB.5
    #define SS_PIN PORTB.4
    //---------------------------------

    #define Block_len 512 //dinh nghia do dai 1 sector

    //Dinh nghia mot so lenh co ban giao tiep voi MMC
    #define CMD0 0x00 //GO_IDLE_STATE, reset va dua vao trang thai nghi
    #define CMD1 0x01 //SEND_OP_COND, Yeu cau Card tra loi ve trang thai cua CARD
    #define CMD16 0x10 //SET_BLOCKLEN, Dat do dai (tinh theo byte) cho 1 khoi du lieu (data block)
    #define CMD17 0x11 //READ_SINGLE_BLOCK, doc 1 block du lieu cua CARD
    #define CMD24 0x18 //WRITE_BLOCK, ghi 1 mang du lieu vao 1 block cua CARD

    char mmc_status=0; //cac trang thai tra ve khi thao tac voi card

    //0: ok
    //1: loi khi reset, timeout khi reset
    //2: loi xay ra luc goi lenh CMD1
    //3: loi khi set blocklen
    //4: timeout ghi goi lend writeblock CMD24
    //5: timeout trong qua trinh write data
    //6: timeout do card dang ban
    //7: loi khi goi lenh readblock, CMD17
    //8: loi khi kiem tra token cua lenh doc du lieu

    //cai dat cho SPI o che do Master------------------
    void SPI_MasterInit(void);
    void SPI_tByte(unsigned char data);
    unsigned char SPI_rByte(void);
    // end of SPI---------------------------------------

    //cac lenh giao tiep mmc***********************************************
    unsigned char mmc_rResponse(unsigned char Response); //yeu cau Response (dap ung) tu card
    void mmc_tCommand(unsigned char Cmd,long arg); //goi 1 lenh den card
    char mmc_init(void); //khoi dong card
    char mmc_writeblock(long LBAddress, unsigned char *buff); //ghi 1 sector (1 khoi du lieu)
    char mmc_readblock(unsigned long LBAddress, unsigned char *buff); //doc 1 sector (1 khoi du lieu)

    //DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD DDDDDDDDDDDDDDDDDDDDDD
    //cai dat cho SPI o che do Master----------------------------------------------------------------------------
    void SPI_MasterInit(void){
    SPI_DDR =0B10110000;//(1<<SCK_PIN)|(1<<MOSI_PIN)|(1<<SS_PIN);
    MISO_PIN=1; //dien tro keo len cho chan MISO
    //chu y la nen set CPOL=0, CPHA=0 (MMC lam viec tot o mode nay)

    SPCR.6=1;
    SPCR.4=1;

    // SPE: enable, MSTR: Master mode, SPR1:0: prescaler=128
    }

    void SPI_tByte(unsigned char data){ //transmite one byte
    SPDR=data;
    while(SPSR.7==0){;} //cho den khi bit SPIF duoc set, qua trinh truyen ket thuc
    }

    unsigned char SPI_rByte(void){ //receive one byte
    unsigned char data;
    SPDR=0xFF;//dummy value
    while(SPSR.7==0); //cho den khi bit SPIF duoc set, qua trinh truyen dummy ket thuc
    data=SPDR;
    return data;

    }
    // end of SPI--------------------------------------------------------------------------------------------

    // for mmc***********************************************
    //nhan va so sanh Response tu mmc
    unsigned char mmc_rResponse(unsigned char Response){
    int Timeout=0x0fff;
    unsigned char Res;
    while((Timeout--)>0){

    Res=SPI_rByte();
    if (Res==Response) break; //escape from while
    }
    if (Timeout==0) return 1; //tra ve 1 neu timeout
    else return 0; // ko co loi, tra ve 0
    }

    void mmc_tCommand(unsigned char Cmd,long arg){
    //command la 48 bit lien tiep trong do
    //bit47=0 (start bit)
    //bit46=1 (transmission bit)
    //bit45:40 la ma lenh
    //bit39:8 la argument cua lenh
    //bit7:1 Cyclic Redundancy Check (CRC)
    //bit0=1 la end bit

    SS_PIN = 0; //kich hoat duong SS cua SPI, MMC duoc chon
    SPI_tByte(0xFF); //dummy, 1 lenh luon bat dau 0 nen send FF thi MMC ko dap ung
    SPI_tByte(Cmd | 0x40); //0x40=01000000 la ma bat buoc khi goi lenh

    SPI_tByte((unsigned char)(arg >>24));
    SPI_tByte((unsigned char)(arg >>16));
    SPI_tByte((unsigned char)(arg >> 8));
    SPI_tByte((unsigned char) arg);

    SPI_tByte(0x95); //CRC, cho lan dau nhung neu cac lan sau send 0x95 cung ko van de;
    SPI_tByte(0xFF); //ko quan cam return
    }

    char mmc_init(void){
    unsigned int i;

    SPI_MasterInit();
    SS_PIN=1; //disable SPI MMC


    for (i=0; i<10; i++) {SPI_tByte(0xFF);} //MMC se vao SPI mode neu duoc nhan nhieu hon 80 clock tren SCK
    SS_PIN=0; //cho phep MMC hoat dong


    mmc_tCommand(CMD0,0); //lenh CMD0. reset mmc

    if(mmc_rResponse(0x00) == 1){ //co loi kiem tra response
    mmc_status=1; // timeout khi reset
    SS_PIN = 1; //disable SPI MMC
    return mmc_status;
    }

    //goi lenh CMD1 cho den khi nhan duoc response =0 hoac timeout
    i=0xffff; //max timeout
    do{
    mmc_tCommand(CMD1,0); //lenh CMD1
    i--;
    } while((SPI_rByte()!=0) && i>0);


    if(i == 1){ //co loi khi goi CMD1
    mmc_status=2; // loi CMD1
    SS_PIN = 1; //disable SPI MMC
    return mmc_status;
    }

    mmc_tCommand(CMD16,Block_len); //lenh CMD16, set do dai sector,Block_len=512
    if(mmc_rResponse(0x00) == 1){ //co loi kiem tra response
    mmc_status=3; // timeout khi set len
    SS_PIN = 1; //disable SPI MMC
    return mmc_status;
    }

    SS_PIN = 1; //disable SPI MMC
    return 0; //no error
    }

    char mmc_writeblock(long LBAddress, unsigned char *buff){
    long tempA,i;

    unsigned char status;
    tempA=512*LBAddress;

    //cach bo tri dia lba cho cho lenh write xxxxxxxx-xxxxxxxx , xxxxxxx0-00000000
    //trong do x la cac bit cua dia chi, nhu vay 9 bit dau cua argument low ko dung
    SS_PIN = 0; //cho phep MMC hoat dong
    mmc_tCommand(CMD24,tempA); //goi lenh cho phep ghi single sector, chu y cach set dia chi

    if(mmc_rResponse(0x00) == 1){ //co loi kiem tra response
    mmc_status=4; // timeout khi goi lenh write block
    SS_PIN = 1; //disable SPI MMC
    return mmc_status;
    }

    //goi dau hieu (token) bao cho mmc biet sap co data den, dau hieu=0xFE
    SPI_tByte(0xFE);

    //goi Block_len=512 byte data
    for (i=0; i<Block_len; i++) {SPI_tByte(buff[i]); }

    //goi 2 byte checksum
    SPI_tByte(0xFF);
    SPI_tByte(0xFF);

    //doc trai thai response, phai la 0x05

    status=SPI_rByte();
    if((status&0x0F) != 0x05){ //co loi kiem tra response
    mmc_status=5; // loi khi goi datablock
    SS_PIN = 1; //disable SPI MMC
    return mmc_status;
    }

    //cho mmc het busy
    i = 0xffff; // max timeout
    while(!SPI_rByte() && (--i)){;} // wait until we are not busy
    if (i==0) {
    mmc_status=6; // loi timeout khi cho busy
    SS_PIN =1; //disable SPI MMC
    return mmc_status;
    }

    SS_PIN = 1; //disable SPI MMC
    return 0;
    }

    char mmc_readblock(unsigned long LBAddress, unsigned char *buff){
    long tempA,i;

    tempA=512*LBAddress;

    //cach bo tri dia lba cho cho lenh write xxxxxxxx-xxxxxxxx , xxxxxxx0-00000000
    //trong do x la cac bit cua dia chi, nhu vay 9 bit dau cua argument low ko dung
    SS_PIN = 0; //cho phep MMC hoat dong
    mmc_tCommand(CMD17,tempA); //goi lenh cho phep doc single sector, chu y cach set dia chi

    if(mmc_rResponse(0x00) == 1){ //co loi kiem tra response
    mmc_status=7; // timeout khi goi len read block
    SS_PIN = 1; //disable SPI MMC
    return mmc_status;
    }

    //kiem tra dau hieu (token) read
    if(mmc_rResponse(0xFE) == 1){ //co loi kiem tra response
    mmc_status=8; // timeout khi goi len read block
    SS_PIN = 1; //disable SPI MMC
    return mmc_status;
    }

    //sau day la goi Block_len=512 byte data
    for (i=0; i<Block_len; i++) buff[i]=SPI_rByte();

    //goi 2 byte checksum
    SPI_tByte(0xFF);
    SPI_tByte(0xFF);

    SS_PIN = 1; //disable SPI MMC
    return 0;
    }
    //XXXXXXXXXXXX fileneme XXXXXXXXX



    //xxxxxxxxxxxxxxxxxxxxxx
    void main(void)
    {
    // Declare your local variables here

    // Input/Output Ports initialization
    // Port A initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTA=0x00;
    DDRA=0x00;

    // Port B initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTB=0xf0;
    DDRB=0xB0;

    // Port C initialization
    // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
    // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
    PORTC=0x00;
    DDRC=0xFF;

    // Port D initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTD=0x00;
    DDRD=0x00;

    // Timer/Counter 0 initialization
    // Clock source: System Clock
    // Clock value: Timer 0 Stopped
    // Mode: Normal top=FFh
    // OC0 output: Disconnected
    TCCR0=0x00;
    TCNT0=0x00;
    OCR0=0x00;

    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: Timer 1 Stopped
    // Mode: Normal top=FFFFh
    // OC1A output: Discon.
    // OC1B output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer 1 Overflow Interrupt: Off
    // Input Capture Interrupt: Off
    // Compare A Match Interrupt: Off
    // Compare B Match Interrupt: Off
    TCCR1A=0x00;
    TCCR1B=0x00;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;

    // Timer/Counter 2 initialization
    // Clock source: System Clock
    // Clock value: Timer 2 Stopped
    // Mode: Normal top=FFh
    // OC2 output: Disconnected
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    // External Interrupt(s) initialization
    // INT0: Off
    // INT1: Off
    // INT2: Off
    MCUCR=0x00;
    MCUCSR=0x00;

    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x00;

    // USART initialization
    // Communication Parameters: 8 Data, 1 Stop, No Parity
    // USART Receiver: On
    // USART Transmitter: On
    // USART Mode: Asynchronous
    // USART Baud rate: 38400
    // SPI
    SPSR=0X00;
    SPCR=0x00;

    // UART
    mmc_init();
    UCSRA=0x00;
    UCSRB=0x18;
    UCSRC=0x86;
    UBRRH=0x00;
    UBRRL=0x11;

    /// Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;
    PORTC=0x55;
    delay_ms(2000);
    PORTC=0xaa;
    n=3;k=1;
    while (1)
    {




    PORTC=0x00;
    for(j=0;j<5;j++){
    delay_ms(1);
    UDR=take[j];
    while(UCSRA.6==0){;}
    }

    delay_ms(2);

    // xxxxx READ SIZE
    for(j=0;j<5;j++){
    delay_ms(1);
    UDR=read_size[j];
    while(UCSRA.6==0){;}
    }

    for(j=0;j<12;j++){
    while(UCSRA.7==0){;}
    data[j]=UDR;
    }

    delay_ms(2);


    //xxxxx READ JPEG
    for(j=0;j<12;j++)
    {
    delay_ms(1);
    UDR=read_JPEG[j];
    while(UCSRA.6==0){;}
    }
    // 160 * 120 JPEG resolution
    delay_ms(1);
    UDR=data[10];
    while(UCSRA.6==0){;}

    delay_ms(1);
    UDR=data[11];
    while(UCSRA.6==0){;}

    delay_ms(1);
    UDR=0x00;
    while(UCSRA.6==0){;}

    delay_ms(1);
    UDR=0x0A;
    while(UCSRA.6==0){;}

    buff[0]=0x00;
    while(buff[0]!=0XFF) {

    while(UCSRA.7==0){;}
    buff[0]=UDR;
    }


    while(1){
    while(UCSRA.7==0){PORTC=0x10;}
    buff[k]=UDR;
    if(k==511)
    {mmc_writeblock(n,buff); n++;k=0;}
    if((buff[k]==0xD9) && (buff[k-1]==0xFF)) {mmc_writeblock(n,buff); n++;k=0;break; }
    else
    k++;
    }
    //xxxxxxxxx stop xxxxxxx
    /* delay_ms(2);
    for(j=0;j<5;j++)
    {
    delay_ms(1);
    UDR=stop[j];
    while(UCSRA.6==0){;}
    }


    PORTC=0x00;
    for(j=0;j<512;j++)
    {buff[j]=0x00;}
    for(k=3;k<=n;k++ ){
    mmc_readblock(k,buff) ;

    //while(1){
    // for(j=0;j<10;j++){
    // PORTC=0x55; delay_ms(500); PORTC=0xaa; delay_ms(500); }

    for(j=0;j<512;j++)
    {if(buff[j]==0) PORTC.7=1;
    else
    PORTC=buff[j];
    delay_ms(100);
    PORTC=0x00;
    delay_ms(100);
    if((buff[j]==0xD9) & (buff[j-1]==0xFF))
    while(1){PORTC=0xaa;delay_ms(1000); PORTC=0x44;delay_ms(1000);}

    }
    } */

    }
    }

  • #2
    các bác cho e hỏi, có thể sử dụng AVR để đọc file Audio từ thẻ nhớ rồi phát ra loa không? e cảm ơn.
    WS:
    Blog:

    Comment

    Về tác giả

    Collapse

    mrhson Tìm hiểu thêm về mrhson

    Bài viết mới nhất

    Collapse

    Đang tải...
    X