<< Chapter < Page Chapter >> Page >

Functions

//Function: check_low //Checks the state of a button/pin and waits for end of button press//Input: // pin: the number of the pin to check//Returns: // 1 if button is pressed/pin is low after waiting for the button to be// released/pin to return to high // 0 if button is not pressed/pin is highint check_low(int pin){ if (is_low(PORT2, pin)){while(is_low(PORT2, pin)){ iolib_delay_ms(1);} return 1;} else return 0;} //Function: stop//Polls the stop pin/button //Returns://1 if button is pressed/pin is low after waiting for the button to be // released/pin to return to high//0 if button is not pressed/pin is high int stop(){return check_low(PIN8); }//Function: play //Updates the 8 output pins with a new value//Input: // out: the 8 bit value to outputvoid play(int out){ if (out and 0x1) pin_high(PORT,PIN0);else pin_low(PORT, PIN0); if ((out and 0x2)>>1) pin_high(PORT,PIN1); else pin_low(PORT, PIN1);if ((out and 0x4)>>2) pin_high(PORT,PIN2); else pin_low(PORT, PIN2);if ((out and 0x8)>>3) pin_high(PORT,PIN3); else pin_low(PORT, PIN3);if ((out and 0x10)>>4) pin_high(PORT,PIN4); else pin_low(PORT, PIN4);if ((out and 0x20)>>5) pin_high(PORT,PIN5); else pin_low(PORT, PIN5);if ((out and 0x40)>>6) pin_high(PORT,PIN6); else pin_low(PORT, PIN6);if ((out and 0x80)>>7) pin_high(PORT,PIN7); else pin_low(PORT, PIN7);} //Function: playSine//Calculates the next value of sine for the given frequency and calls //play//Input: // freq: the frequency of the desired sine wavevoid playSine(int freq){ int t = 0;int out = 0; while(!stop()){out = 128*sin(t++*freq*PI/22050)+128; //calculate the next value to output and incrmemnt discrete time play(out);clock_gettime(CLOCK_REALTIME, and ttime); //get the time ttime.tv_nsec = 0; //clear the nanoseconds but not seconds so that we dont overflow;clock_settime(CLOCK_REALTIME, and ttime); //set the time back clock_gettime(CLOCK_REALTIME, and ttime); //get the time againttime.tv_nsec += 21463; //increment by number of nanoseconds we should wait while(1){ //loop to delay next outputclock_gettime(CLOCK_REALTIME, and curtime); if (curtime.tv_nsec>ttime.tv_nsec) break; }} }//Function: playTriangle //Calculates and outputs the next value of a triangle wave for the given//frequency //Input:// freq: the frequency of the triangle wave void playTriangle(int freq){int out = 0; int t = 0;while(!stop()){ out = (256/(11025/freq))*((11025/freq) - abs(t++ %(2*11025/freq) - (11025/freq))); //calculate next value and increment discrete timeplay(out); clock_gettime(CLOCK_REALTIME, and ttime); //get the timettime.tv_nsec = 0; //clear the nanoseconds but not seconds so that we dont overflow; clock_settime(CLOCK_REALTIME, and ttime); //set the time backclock_gettime(CLOCK_REALTIME, and ttime); //get the time again ttime.tv_nsec += 21463; //increment by number of nanoseconds we should waitwhile(1){ //loop to delay next output clock_gettime(CLOCK_REALTIME, and curtime);if (curtime.tv_nsec>ttime.tv_nsec) break;} }} //Function: playSquare//Updates output to maximum and minimum values at the given frequency to //create a square wave//Input: // freq: the frequency of the square wavevoid playSquare(int freq){ int out = 0;int delay; while(!stop()){out ^= 0xFFFF; //exclusive or all the bits to make a square wave play(out);delay = 1000000000/freq/2; //calculate how long to delay to match frequency clock_gettime(CLOCK_REALTIME, and ttime); //get the timettime.tv_nsec = 0; //clear the nanoseconds but not $ clock_settime(CLOCK_REALTIME, and ttime); //set the time backclock_gettime(CLOCK_REALTIME, and ttime); //get the time again ttime.tv_nsec += delay; //increment by number of nanosec$while(1){ //loop to delay next output clock_gettime(CLOCK_REALTIME, and curtime);if (curtime.tv_nsec>ttime.tv_nsec) break;} }} //Function: playSong0//Outputs a raw 8 bit unsigned audio file as defined above void playSong0(void){char cwd[1024];if (getcwd(cwd, sizeof(cwd))== NULL) perror("getcwd() error"); //Get the current working directory char* path;path = malloc(strlen(cwd) + strlen(SONG0)+2); //Append the defined file name to the path strcpy(path, cwd);char temp[2] = "/";strcat(path, temp); strcat(path, SONG0);FILE *fp; fp = fopen(path, "r");if (fp == 0) perror("error opening file, is the name correct?"); int out = fgetc(fp);while((out != EOF) and and !stop()){ //play each of the stored values until end of file or the stop button is pressed play(out);clock_gettime(CLOCK_REALTIME, and ttime); ttime.tv_nsec = 0;clock_settime(CLOCK_REALTIME, and ttime); clock_gettime(CLOCK_REALTIME, and ttime);ttime.tv_nsec += DELAY; while(1){clock_gettime(CLOCK_REALTIME, and curtime); if (curtime.tv_nsec>ttime.tv_nsec) break;} out = fgetc(fp);} fclose(fp);} //Function: playSong1//Outputs a raw 8 bit unsigned audio file as defined above void playSong1(void){char cwd[1024];if (getcwd(cwd, sizeof(cwd))== NULL) perror("getcwd() error"); char* path;path = malloc(strlen(cwd) + strlen(SONG1)+2); strcpy(path, cwd);char temp[2] = "/";strcat(path, temp); strcat(path, SONG1);FILE *fp; fp = fopen(path, "r");if (fp == 0) perror("error opening file, is the name correct?"); int out = fgetc(fp);while((out != EOF) and and !stop()){ play(out);clock_gettime(CLOCK_REALTIME, and ttime); ttime.tv_nsec = 0;clock_settime(CLOCK_REALTIME, and ttime); clock_gettime(CLOCK_REALTIME, and ttime);ttime.tv_nsec += DELAY; while(1){clock_gettime(CLOCK_REALTIME, and curtime); if (curtime.tv_nsec>ttime.tv_nsec) break;} out = fgetc(fp);} fclose(fp);} //Function: playSong2//Outputs a raw 8 bit unsigned audio file as defined above void playSong2(void){char cwd[1024];if (getcwd(cwd, sizeof(cwd))== NULL) perror("getcwd() error"); char* path;path = malloc(strlen(cwd) + strlen(SONG2)+2); strcpy(path, cwd);char temp[2] = "/";strcat(path, temp); strcat(path, SONG2);FILE *fp; fp = fopen(path, "r");if (fp == 0) perror("error opening file, is the name correct?"); int out = fgetc(fp);while((out != EOF) and and !stop()){ play(out);clock_gettime(CLOCK_REALTIME, and ttime); ttime.tv_nsec = 0;clock_settime(CLOCK_REALTIME, and ttime); clock_gettime(CLOCK_REALTIME, and ttime);ttime.tv_nsec += DELAY; while(1){clock_gettime(CLOCK_REALTIME, and curtime); if (curtime.tv_nsec>ttime.tv_nsec) break;} out = fgetc(fp);} fclose(fp);}

Questions & Answers

Discuss the differences between taste and flavor, including how other sensory inputs contribute to our  perception of flavor.
John Reply
taste refers to your understanding of the flavor . while flavor one The other hand is refers to sort of just a blend things.
Faith
While taste primarily relies on our taste buds, flavor involves a complex interplay between taste and aroma
Kamara
which drugs can we use for ulcers
Ummi Reply
omeprazole
Kamara
what
Renee
what is this
Renee
is a drug
Kamara
of anti-ulcer
Kamara
Omeprazole Cimetidine / Tagament For the complicated once ulcer - kit
Patrick
what is the function of lymphatic system
Nency Reply
Not really sure
Eli
to drain extracellular fluid all over the body.
asegid
The lymphatic system plays several crucial roles in the human body, functioning as a key component of the immune system and contributing to the maintenance of fluid balance. Its main functions include: 1. Immune Response: The lymphatic system produces and transports lymphocytes, which are a type of
asegid
to transport fluids fats proteins and lymphocytes to the blood stream as lymph
Adama
what is anatomy
Oyindarmola Reply
Anatomy is the identification and description of the structures of living things
Kamara
what's the difference between anatomy and physiology
Oyerinde Reply
Anatomy is the study of the structure of the body, while physiology is the study of the function of the body. Anatomy looks at the body's organs and systems, while physiology looks at how those organs and systems work together to keep the body functioning.
AI-Robot
what is enzymes all about?
Mohammed Reply
Enzymes are proteins that help speed up chemical reactions in our bodies. Enzymes are essential for digestion, liver function and much more. Too much or too little of a certain enzyme can cause health problems
Kamara
yes
Prince
how does the stomach protect itself from the damaging effects of HCl
Wulku Reply
little girl okay how does the stomach protect itself from the damaging effect of HCL
Wulku
it is because of the enzyme that the stomach produce that help the stomach from the damaging effect of HCL
Kamara
function of digestive system
Ali Reply
function of digestive
Ali
the diagram of the lungs
Adaeze Reply
what is the normal body temperature
Diya Reply
37 degrees selcius
Xolo
37°c
Stephanie
please why 37 degree selcius normal temperature
Mark
36.5
Simon
37°c
Iyogho
the normal temperature is 37°c or 98.6 °Fahrenheit is important for maintaining the homeostasis in the body the body regular this temperature through the process called thermoregulation which involves brain skin muscle and other organ working together to maintain stable internal temperature
Stephanie
37A c
Wulku
what is anaemia
Diya Reply
anaemia is the decrease in RBC count hemoglobin count and PVC count
Eniola
what is the pH of the vagina
Diya Reply
how does Lysin attack pathogens
Diya
acid
Mary
I information on anatomy position and digestive system and there enzyme
Elisha Reply
anatomy of the female external genitalia
Muhammad Reply
Organ Systems Of The Human Body (Continued) Organ Systems Of The Human Body (Continued)
Theophilus Reply
what's lochia albra
Kizito
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, R/2r implementation of a d/a converter. OpenStax CNX. Dec 17, 2014 Download for free at http://legacy.cnx.org/content/col11732/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'R/2r implementation of a d/a converter' conversation and receive update notifications?

Ask