Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX7456.cpp Source File

MAX7456.cpp

00001 #include "mbed.h"
00002 #include "MAX7456.h"
00003 
00004 namespace mbed {
00005 
00006 
00007 
00008 MAX7456::MAX7456(PinName mosi, PinName miso, PinName clk, PinName ncs, PinName nrst, const char* name)
00009         : Stream(name), _spi(mosi, miso, clk), _ncs(ncs),_nrst(nrst){
00010 
00011 // initialisation code here
00012 
00013     _nrst = 0;
00014     wait (0.5);
00015     _nrst = 1;
00016     wait (0.5);
00017 
00018     // Setup the spi for 8 bit data, high steady state clock,
00019     // second edge capture
00020     _spi.format(8,0);
00021 
00022     // 1MHz clock
00023     _spi.frequency(1000000);
00024 }
00025 
00026 
00027 void MAX7456::cls() {
00028     int tmp=0;
00029     tmp = _read(DMM);
00030     tmp &= 0xFB;
00031     _write(DMM,tmp);  //Make sure that DMM[2]=0 so that there can be write operations
00032 
00033     tmp = _read(DMM);
00034     tmp |= 0x04;
00035     _write (DMM,tmp);   //set DMM[2]=1 to clear all locations
00036 
00037     // should wait until DMM[2] goes back to zero, so we know the reset it finished
00038 
00039 }
00040 
00041 /*
00042 void MAX7456::show(){
00043    _write(DMAL,0x1e); // location (0,1)
00044    _write(DMDI,0x21); // writing the charater address "W" into DMDI 
00045 
00046    _write(DMAL,0x1f); // location (0,1)
00047    _write(DMDI,0x19); // writing the charater address "O" into DMDI 
00048 
00049    _write(DMAL,0x20); // location (0,1)
00050    _write(DMDI,0x1C); // writing the charater address "R" into DMDI 
00051 
00052    _write(DMAL,0x21); // location (0,1)
00053    _write(DMDI,0x16); // writing the charater address "L" into DMDI 
00054 
00055    _write(DMAL,0x22); // location (0,1)
00056    _write(DMDI,0x0E); // writing the charater address "D" into DMDI  0x0E
00057    } 
00058 */
00059 
00060 int MAX7456::_getc() {    
00061     int mode= _read(DMM);
00062     mode|=0x01;
00063     _write(DMM,mode);
00064    return(0);
00065 }
00066 void MAX7456::test(){
00067    for (int i = 0 ; i < 255 ; i++) {
00068        printf("Writing 0x%x to VM1\n",i);
00069        _write(VM1,i);
00070        wait (0.01);
00071        int a = _read(VM1);
00072        printf("Read 0x%x from VM1\n",a);       
00073    }
00074    int b = _read(VM1);
00075    printf("Read 0x%x from VM1\n",b);
00076    }
00077 
00078 void MAX7456::locate(int x, int y) { 
00079     if ( (x<30) && (y<16) ) {
00080         int add = y*30+x; //formula for converting coordinates into denary location
00081         _write(DMAL,add);
00082         _write(DMAH,add>>8); // what does the ">>" mean?
00083     }
00084      printf("location %d",x);
00085 }
00086 
00087 
00088 void MAX7456::xyincrement(){ // increment starts here
00089     int x=0;
00090     int y=0;
00091     while (x<31&&y<=16){
00092         x=x+1;
00093         if (x>30){
00094         y=y+1;
00095         x=0;
00096         }
00097         locate(x,y);
00098     }
00099 }
00100 
00101     
00102 
00103 int MAX7456::_putc(int c) {
00104     int set= _read(DMM);
00105     set&=0xFB;           //set bit 1 to 0 so that the DMM register can be written to
00106     _write(DMM,set);  
00107 
00108     
00109     if ((c >= 0x31)&&(c <= 0x39)) { //characters from 1-9
00110         c= c-0x30;
00111     } 
00112     else if ((c >= 0x41)&&(c <= 0x5A)) { // characters from A-Z
00113         c= c-0x36;
00114     } 
00115     else if ((c >= 0x61)&&(c <= 0x7A)) { // characters from a-z
00116         c= c-0x3C;
00117     } 
00118     else if ((c == 0x28)&&(c == 0x29)) { //brackets ()
00119         c= c + 0x17;
00120     }
00121 
00122     _write(DMDI,c);
00123     
00124     /*
00125     int mode= _read(DMM);
00126     mode|=0x01;        // enable auto increment mode
00127     _write(DMM,mode);
00128     _HYSNC=1;
00129     */
00130     //xyincrement();
00131     
00132     /*
00133     int data= _read(DMDI);
00134     printf("DMDI shows 0x%x",data);
00135     */
00136     
00137     return(c);
00138  
00139 }
00140 
00141 void MAX7456::setdarkness() {
00142     _write(VM1,0xC7);
00143 }
00144 
00145 void MAX7456::invert(int bit) {
00146     int data= _read(DMM);
00147     if (bit == 1){
00148         data|=0x08;
00149         }
00150     else if (bit == 0){
00151         data&=0xF9;
00152         }
00153     _write(DMM,data);
00154     }
00155         
00156 
00157 
00158 void MAX7456::initaddress() {
00159     int d = _read(DMAH);
00160     d= d&0xFC;
00161     _write(DMAH,d);     // setting DMAH[1]=0
00162     // to module to read/write character address byte
00163 }
00164 
00165 
00166 
00167 //   write(HOS,0x2F);
00168 //   write(VOS,0x17);
00169 void MAX7456::vtrim(char way, int v) {
00170     if (way=='+'){
00171         v=~v;
00172         _write(VOS,v);
00173         }
00174     else{
00175         v|=0x10;
00176         _write(VOS,v);
00177         }        
00178 }
00179 
00180 void MAX7456::htrim(char way, int h) {
00181     if (way=='-'){
00182         h=~h;
00183         _write(HOS,h);
00184         }
00185     else{
00186         h|=0x20;
00187         _write(HOS,h);
00188         }     
00189 }
00190 
00191 
00192 
00193 void MAX7456::format (char mode) {
00194 
00195     if (mode == 'P') {
00196         _write(VM0,0x78);  // PAL
00197     } else if (mode == 'N') {
00198         _write(VM0,0x78);  // NTSC
00199     }
00200 
00201 }
00202 
00203 int MAX7456::_read(int address) {
00204 // force bit 7 to 1 for a read
00205     address |= 0x80;
00206     _ncs=0; // select device
00207     _spi.write(address);          // send address
00208     int value = _spi.write(0x00); // send dummy
00209     _ncs=1; //deselect device
00210     return (value);
00211 }
00212 
00213 void MAX7456::_write(int address, int data) {
00214     // force bit 7 to 0 for a write
00215     address &= 0x7f;
00216     // select the device
00217     _ncs = 0;
00218     // write VM1
00219     _spi.write(address); // send address
00220     _spi.write(data); // send some data
00221     // Deselect the device
00222     _ncs = 1;
00223 }
00224 
00225 }