You are a highly proficient firmware engineer with expertise in firmware development and…
- User
You are a highly proficient firmware engineer with expertise in firmware development and coding.
Generate me some codes in C
User: Generate me some codes in C
- Assistant
Sure! Here are a few examples of C code snippets that showcase different functionalities. Let me know if you need something more specific or tailored to your needs.
Example 1: Blinking an LED (Embedded System Example)
This code is for an embedded system that blinks an LED connected to a microcontroller.
#include <avr/io.h> #include <util/delay.h> #define LED_PIN PB0 // Define LED pin int main(void) { // Set LED_PIN as output DDRB |= (1 << LED_PIN); while (1) { // Turn LED on PORTB |= (1 << LED_PIN); _delay_ms(500); // Delay for 500 milliseconds // Turn LED off PORTB &= ~(1 << LED_PIN); _delay_ms(500); // Delay for 500 milliseconds } return 0; }Example 2: A Simple UART Communication
This example shows basic UART initialization and sending a string.
#include <avr/io.h> #include <util/delay.h> void UART_Init(unsigned int baud) { // Set baud rate unsigned int ubrr = F_CPU / 16 / baud - 1; UBRR0H = (unsigned char)(ubrr >> 8); UBRR0L = (unsigned char)(ubrr); // Enable receiver and transmitter UCSR0B = (1 << RXEN0) | (1 << TXEN0); } void UART_Transmit(unsigned char data) { // Wait for empty transmit buffer while (!(UCSR0A & (1 << UDRE0))); // Put data into buffer, sends the data UDR0 = data; } void UART_SendString(const char* str) { while (*str) { UART_Transmit(*str++); } } int main(void) { UART_Init(9600); // Initialize UART with 9600 baud rate while (1) { UART_SendString("Hello World!\n"); _delay_ms(1000); } return 0; }Example 3: I2C Communication Example
This is a simple example of how to set up and use I2C to communicate with a peripheral.
#include <avr/io.h> #include <util/twi.h> void TWI_Init(void) { // Set the SCL frequency to 100kHz TWSR = 0; // Prescaler = 1 TWBR = 32; // SCL frequency = CPU_frequency / (16 + 2*TWBR*4^TWSR) } void TWI_Start(void) { TWCR = (1 << TWSTA) | (1 << TWEN) | (1 << TWINT); while (!(TWCR & (1 << TWINT))); } void TWI_Stop(void) { TWCR = (1 << TWSTO) | (1 << TWINT) | (1 << TWEN); } void TWI_Write(uint8_t data) { TWDR = data; TWCR = (1 << TWINT) | (1 << TWEN); while (!(TWCR & (1 << TWINT))); } int main(void) { TWI_Init(); TWI_Start(); TWI_Write(0xA0); // Example I2C address TWI_Stop(); while (1) { // Main loop } return 0; }Example 4: Simple HTTP Server (Using a TCP Socket)
This example demonstrates how to create a simple HTTP server using sockets.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 8080 #define BUF_SIZE 1024 int main() { int server_fd, new_socket; struct sockaddr_in address; int addrlen = sizeof(address); char buffer[BUF_SIZE] = {0}; const char *hello = "HTTP/1.1 200 OK\nContent-Type: text/html\n\nHello World"; // Creating socket file descriptor if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); // Bind if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("bind failed"); exit(EXIT_FAILURE); } // Listen if (listen(server_fd, 3) < 0) { perror("listen failed"); exit(EXIT_FAILURE); } while (1) { printf("Waiting for connections...\n"); if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { perror("accept failed"); exit(EXIT_FAILURE); } read(new_socket, buffer, BUF_SIZE); printf("Received request:\n%s\n", buffer); send(new_socket, hello, strlen(hello), 0); printf("Hello message sent\n"); close(new_socket); } return 0; }These examples cover a range of functionalities, from hardware interfacing in embedded systems to network programming. If you have a specific application or feature in mind, let me know!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.